This commit is contained in:
John Alanbrook 2024-01-23 16:56:34 +00:00
parent dde71d6c02
commit 46b86a8e92
4 changed files with 98 additions and 77 deletions

View file

@ -162,9 +162,17 @@ JSValue js_getpropidx(JSValue v, uint32_t i)
static inline cpBody *js2body(JSValue v) { return js2gameobject(v)->body; }
uint64_t js2uint64(JSValue v) { return JS_VALUE_GET_INT(v); }
uint64_t js2uint64(JSValue v) {
uint64_t i;
JS_ToInt64(js, &i, v);
return i;
}
int js2int(JSValue v) { return JS_VALUE_GET_INT(v); }
int js2int(JSValue v) {
int i;
JS_ToInt32(js, &i, v);
return i;
}
JSValue int2js(int i) { return JS_NewInt64(js, i); }
@ -227,49 +235,61 @@ int js_arrlen(JSValue v) {
char *js_nota_decode(JSValue *tmp, char *nota)
{
int type = nota_type(nota);
JSValue ret;
JSValue ret2;
long long n;
double d;
int b;
char *str = NULL;
char *str;
switch(type) {
case NOTA_BLOB:
break;
case NOTA_TEXT:
nota = nota_read_text(str, nota);
nota = nota_read_text(&str, nota);
*tmp = str2js(str);
return nota;
break;
case NOTA_ARR:
nota = nota_read_array(&n, nota);
ret = JS_NewArray(js);
*tmp = JS_NewArray(js);
for (int i = 0; i < n; i++) {
nota = js_nota_decode(&ret2, nota);
JS_SetPropertyInt64(js, ret, i, ret2);
JS_SetPropertyInt64(js, *tmp, i, ret2);
}
break;
case NOTA_REC:
nota = nota_read_record(&n, nota);
printf("nota object with %d elements\n", n);
*tmp = JS_NewObject(js);
for (int i = 0; i < n; i++) {
nota = nota_read_text(&str, nota);
printf("looking at property %d named %s\n", i, str);
nota = js_nota_decode(&ret2, nota);
JS_SetPropertyStr(js, *tmp, str, ret2);
}
break;
case NOTA_FLOAT:
nota = nota_read_float(&d, nota);
*tmp = number2js(d);
return nota;
case NOTA_INT:
nota = nota_read_int(&n, nota);
*tmp = int2js(n);
return nota;
break;
case NOTA_SYM:
nota = nota_read_bool(&b, nota);
*tmp = bool2js(b);
return nota;
break;
default:
case NOTA_FLOAT:
nota = nota_read_float(&d, nota);
*tmp = number2js(d);
break;
}
return nota;
}
char *js_nota_encode(JSValue v, char *nota)
{
int tag = JS_VALUE_GET_TAG(v);
char *str;
char *str = NULL;
JSPropertyEnum *ptab;
uint32_t plen;
int n;
@ -277,28 +297,40 @@ char *js_nota_encode(JSValue v, char *nota)
switch(tag) {
case JS_TAG_FLOAT64:
// printf("encode float\n");
return nota_write_float(JS_VALUE_GET_FLOAT64(v), nota);
case JS_TAG_INT:
// printf("encode int\n");
return nota_write_int(JS_VALUE_GET_INT(v), nota);
case JS_TAG_STRING:
return nota_write_text(JS_VALUE_GET_PTR(v), nota);
// printf("encode string\n");
str = js2str(v);
nota = nota_write_text(str, nota);
JS_FreeCString(js, str);
return nota;
case JS_TAG_BOOL:
// printf("encode bool\n");
return nota_write_bool(JS_VALUE_GET_BOOL(v), nota);
case JS_TAG_OBJECT:
if (JS_IsArray(js, v)) {
// printf("encode array\n");
int n = js_arrlen(v);
nota = nota_write_array(n, nota);
for (int i = 0; i < js_arrlen(v); i++)
nota = js_nota_encode(js_arridx(v, i), nota);
return nota;
}
// printf("encode object\n");
n = JS_GetOwnPropertyNames(js, &ptab, &plen, v, JS_GPN_ENUM_ONLY | JS_GPN_STRING_MASK);
nota = nota_write_record(plen, nota);
for (int i = 0; i < plen; i++) {
val = JS_GetProperty(js,v,ptab[i].atom);
if (JS_IsUndefined(val) || JS_IsNull(val)) continue;
/* todo: slower than atomtocstring */
val = JS_AtomToString(js, ptab[i].atom);
nota = nota_write_text(JS_VALUE_GET_PTR(v), nota);
str = JS_AtomToCString(js, ptab[i].atom);
// printf("encoding object entry %s\n", str);
nota = nota_write_text(str, nota);
nota = js_nota_encode(JS_GetProperty(js, v, ptab[i].atom), nota);
}
return nota;
@ -492,7 +524,6 @@ struct rect js2rect(JSValue v) {
return rect;
}
JSValue rect2js(struct rect rect) {
JSValue obj = JS_NewObject(js);
js_setprop_str(obj, "x", JS_NewFloat64(js, rect.x));
@ -2012,16 +2043,16 @@ JSValue nota_encode(JSContext *js, JSValueConst this, int argc, JSValueConst *ar
JSValue obj = argv[0];
char nota[100000];
char *e = js_nota_encode(obj, nota);
*e = 0;
return JS_NewStringLen(js, nota, e-nota);
return JS_NewArrayBufferCopy(js, nota, e-nota);
}
JSValue nota_decode(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
{
if (argc < 1) return JS_UNDEFINED;
char *nota = js2str(argv[0]);
size_t len;
char *nota = JS_GetArrayBuffer(js, &len, argv[0]);
JSValue ret;
js_nota_decode(&ret, nota);
return ret;

View file

@ -3,6 +3,7 @@
#include "math.h"
#include "string.h"
#include "stdlib.h"
#include "limits.h"
#define NOTA_CONT 0x80
#define NOTA_DATA 0x7f
@ -31,18 +32,26 @@ char *nota_skip(char *nota)
return nota+1;
}
char *nota_read_num(long long *n, char *nota)
{
if (!n)
return nota_skip(nota);
*n = 0;
*n |= (*nota) & NOTA_HEAD_DATA;
while (CONTINUE(*(nota++)))
*n = (*n<<7) | (*nota) & NOTA_DATA;
return nota;
}
int nota_bits(long long n, int sb)
{
int bits;
if (n == 0)
bits = 0;
else
bits = ilogb(n)+1;
if (n == 0) return sb;
int bits = sizeof(n)*CHAR_BIT - __builtin_clzll(n);
bits-=sb; /* start bit */
int chars = bits/7;
if (bits%7>0) chars++;
bits = sb + (chars*7);
return bits;
return ((bits + 6) / 7)*7 + sb;
}
char *nota_continue_num(long long n, char *nota, int sb)
@ -54,10 +63,10 @@ char *nota_continue_num(long long n, char *nota, int sb)
else
nota[0] &= ~NOTA_CONT;
int shex = ~0 << sb;
int shex = (~0) << sb;
nota[0] &= shex; /* clear shex bits */
nota[0] |= ~shex & (n>>bits);
nota[0] |= (~shex) & (n>>bits);
int i = 1;
while (bits > 0) {
bits -= 7;
@ -109,19 +118,6 @@ char *nota_write_int(long long n, char *nota)
return nota_continue_num(n, nota, 3);
}
char *nota_read_num(long long *n, char *nota)
{
if (!n)
return nota_skip(nota);
*n = 0;
*n |= (*nota) & NOTA_HEAD_DATA;
while (CONTINUE(*(nota++)))
*n = (*n<<7) | (*nota) & NOTA_DATA;
return nota;
}
#define NOTA_DBL_PREC 6
#define xstr(s) str(s)
@ -137,7 +133,7 @@ char *nota_write_float(double n, char *nota)
char ns[2+NOTA_DBL_PREC+5];
snprintf(ns, 2+NOTA_DBL_PREC+5, "%." xstr (NOTA_DBL_PREC) "e", n);
int e = atoi(&ns[2+NOTA_DBL_PREC+1]);
long long e = atoll(&ns[2+NOTA_DBL_PREC+1]);
ns[2+NOTA_DBL_PREC] = 0;
char *z = ns + 1 + NOTA_DBL_PREC;
@ -160,15 +156,13 @@ char *nota_write_float(double n, char *nota)
nota[0] = NOTA_FLOAT;
nota[0] |= 0x10 & expsign;
nota[0] |= 0x08 & sign;
char *c = nota_continue_num(e, nota, 3);
char *c = nota_continue_num(e, nota, 3);
return nota_continue_num(sig, c, 7);
}
char *nota_read_float(double *d, char *nota)
{
print_nota_hex(nota);
long long sig = 0;
long long e = 0;
@ -182,17 +176,15 @@ char *nota_read_float(double *d, char *nota)
c++;
sig = (*c) & NOTA_DATA;
while (CONTINUE(*c)) {
do
sig = (sig<<7) | *c & NOTA_DATA;
c++;
}
while (CONTINUE(*(c++)));
if (NOTA_SIG_SIGN(*nota)) sig *= -1;
if (NOTA_EXP_SIGN(*nota)) e *= -1;
*d = (double)sig * pow(10.0, e);
return nota;
return c;
}
char *nota_read_int(long long *n, char *nota)
@ -200,14 +192,15 @@ char *nota_read_int(long long *n, char *nota)
if (!n)
return nota_skip(nota);
*n = 0;
char *c = nota;
*n |= (*c) & NOTA_INT_DATA; /* first three bits */
while (CONTINUE(*(c++)))
*n = (*n<<7) | (*c) & NOTA_DATA;
if (NOTA_INT_SIGN(*nota)) *n *= -1;
return c+1;
return c;
}
/* n is the number of bits */
@ -310,13 +303,7 @@ void encode_kim(char **s, char *end, int code)
return;
}
int bits = 32 - __builtin_clz(code);
if (bits <= 7)
bits = 7;
else if (bits <= 14)
bits = 14;
else
bits = 21;
int bits = ((32 - __builtin_clz(code) + 6) / 7) * 7;
while (bits > 7) {
bits -= 7;
@ -339,10 +326,12 @@ int decode_kim(char **s)
return rune;
}
void utf8_to_kim(char *utf, char *kim)
char *utf8_to_kim(char *utf, char *kim)
{
while (*utf)
encode_kim(&kim, NULL, decode_utf8(&utf));
return kim;
}
void kim_to_utf8(char *kim, char *utf, int runes)
@ -353,21 +342,20 @@ void kim_to_utf8(char *kim, char *utf, int runes)
*utf = 0;
}
char *nota_read_text(char *text, char *nota)
char *nota_read_text(char **text, char *nota)
{
long long chars;
nota = nota_read_num(&chars, nota);
char utf[chars*4];
kim_to_utf8(nota, utf, chars);
text = strdup(utf);
*text = strdup(utf);
return nota;
}
char *nota_write_bool(int b, char *nota)
{
*nota = NOTA_SYM | (b ? NOTA_TRUE : NOTA_FALSE);
nota++;
return nota;
return nota+1;
}
char *nota_read_bool(int *b, char *nota)
@ -382,7 +370,6 @@ char *nota_write_text(char *s, char *nota)
nota[0] = NOTA_TEXT;
long long n = utf8_count(s);
nota = nota_continue_num(n,nota,4);
utf8_to_kim(s, nota);
return nota+n;
return utf8_to_kim(s, nota);
}

View file

@ -9,17 +9,21 @@
#define NOTA_INT 0x60
#define NOTA_SYM 0x70
typedef struct NOTA {
char *head;
} NOTA;
int nota_type(char *nota);
char *nota_read_blob(long long *len, char *nota);
char *nota_read_text(char *text, char *nota);
char *nota_read_text(char **text, char *nota);
char *nota_read_array(long long *len, char *nota);
char *nota_read_record(long long *len, char *nota);
char *nota_read_float(double *d, char *nota);
char *nota_read_int(long long *l, char *nota);
char *nota_read_bool(int *b, char *nota);
char *nota_read_num(long long *n, char *nota);
void print_nota_hex(char *nota);
char *nota_write_blob(unsigned long long n, char *nota);
char *nota_write_text(char *s, char *nota);

View file

@ -11,7 +11,6 @@
#include <stdio.h>
#include "particle.h"
#include "simplex.h"
#include "nota.h"
#include "datastream.h"