Fix nota implementation; add nota test suite

This commit is contained in:
2025-02-23 16:06:40 -06:00
parent fb10c63882
commit 7ea79c8ced
7 changed files with 494 additions and 143 deletions

View File

@@ -4,6 +4,8 @@
#define NOTA_IMPLEMENTATION
#include "nota.h"
JSValue number;
char *js_do_nota_decode(JSContext *js, JSValue *tmp, char *nota)
{
int type = nota_type(nota);
@@ -12,9 +14,13 @@ char *js_do_nota_decode(JSContext *js, JSValue *tmp, char *nota)
double d;
int b;
char *str;
uint8_t *blob;
switch(type) {
case NOTA_BLOB:
nota = nota_read_blob(&n, &blob, nota);
*tmp = JS_NewArrayBufferCopy(js, blob, n);
free(blob);
break;
case NOTA_TEXT:
nota = nota_read_text(&str, nota);
@@ -46,9 +52,17 @@ char *js_do_nota_decode(JSContext *js, JSValue *tmp, char *nota)
break;
case NOTA_SYM:
nota = nota_read_sym(&b, nota);
if (b == NOTA_NULL) *tmp = JS_UNDEFINED;
else
*tmp = JS_NewBool(js,b);
switch(b) {
case NOTA_NULL:
*tmp = JS_UNDEFINED;
break;
case NOTA_FALSE:
*tmp = JS_NewBool(js,0);
break;
case NOTA_TRUE:
*tmp = JS_NewBool(js,1);
break;
}
break;
default:
case NOTA_FLOAT:
@@ -67,27 +81,41 @@ char *js_do_nota_encode(JSContext *js, JSValue v, char *nota)
const char *str = NULL;
JSPropertyEnum *ptab;
uint32_t plen;
int n;
double nval;
JSValue val;
void *blob;
size_t bloblen;
switch(tag) {
case JS_TAG_FLOAT64:
case JS_TAG_INT:
case JS_TAG_BIG_DECIMAL:
case JS_TAG_BIG_INT:
case JS_TAG_BIG_FLOAT:
JS_ToFloat64(js, &nval, v);
return nota_write_number(nval, nota);
/* str = JS_ToCString(js,v);
nota = nota_write_decimal_str(str, nota);
JS_FreeCString(js,str);
return nota;
*/
case JS_TAG_STRING:
str = JS_ToCString(js, v);
nota = nota_write_text(str, nota);
JS_FreeCString(js, str);
return nota;
case JS_TAG_BOOL:
return nota_write_sym(JS_VALUE_GET_BOOL(v), nota);
if (JS_VALUE_GET_BOOL(v)) return nota_write_sym(NOTA_TRUE, nota);
else
return nota_write_sym(NOTA_FALSE, nota);
case JS_TAG_UNDEFINED:
return nota_write_sym(NOTA_NULL, nota);
case JS_TAG_NULL:
return nota_write_sym(NOTA_NULL, nota);
case JS_TAG_OBJECT:
blob = JS_GetArrayBuffer(js,&bloblen, v);
if (blob)
return nota_write_blob(bloblen*8, blob, nota);
if (JS_IsArray(js, v)) {
int n;
JS_ToInt32(js, &n, JS_GetPropertyStr(js, v, "length"));
@@ -96,7 +124,7 @@ char *js_do_nota_encode(JSContext *js, JSValue v, char *nota)
nota = js_do_nota_encode(js, JS_GetPropertyUint32(js, v, i), nota);
return nota;
}
n = JS_GetOwnPropertyNames(js, &ptab, &plen, v, JS_GPN_ENUM_ONLY | JS_GPN_STRING_MASK);
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++) {
@@ -140,18 +168,9 @@ JSValue js_nota_decode(JSContext *js, JSValue self, int argc, JSValue *argv)
return ret;
}
JSValue js_nota_hex(JSContext *js, JSValue self, int argc, JSValue *argv)
{
size_t len;
unsigned char *nota = JS_GetArrayBuffer(js, &len, argv[0]);
print_nota_hex(nota);
return JS_UNDEFINED;
}
static const JSCFunctionListEntry js_nota_funcs[] = {
JS_CFUNC_DEF("encode", 1, js_nota_encode),
JS_CFUNC_DEF("decode", 1, js_nota_decode),
JS_CFUNC_DEF("hex", 1, js_nota_hex),
};
static int js_nota_init(JSContext *ctx, JSModuleDef *m) {
@@ -163,6 +182,7 @@ JSValue js_nota_use(JSContext *js)
{
JSValue export = JS_NewObject(js);
JS_SetPropertyFunctionList(js, export, js_nota_funcs, sizeof(js_nota_funcs)/sizeof(JSCFunctionListEntry));
number = JS_GetPropertyStr(js, JS_GetGlobalObject(js), "Number");
return export;
}