nota quickjs

This commit is contained in:
John Alanbrook 2024-01-16 13:29:27 +00:00
parent bf57e33629
commit 145d5391a0
4 changed files with 230 additions and 104 deletions

View file

@ -26,6 +26,8 @@
#include "resources.h" #include "resources.h"
#include <sokol/sokol_time.h> #include <sokol/sokol_time.h>
#include "nota.h"
#include "render.h" #include "render.h"
#include "model.h" #include "model.h"
@ -1931,97 +1933,75 @@ JSValue duk_profile(JSContext *js, JSValueConst this, int argc, JSValueConst *ar
return JS_UNDEFINED; return JS_UNDEFINED;
} }
#define GETBIT(BYTE,BIT) (BYTE >> (BIT-1) & 1)
#define WRITEBITS(TO,FROM,TOOFFSET,FROMOFFSET,BITS) (
#define NOTA_CONT(BYTE) GETBIT(BYTE,1)
#define NOTA_BLOB(BYTE) (!GETBIT(BYTE,2) && !GETBIT(BYTE,3) && !GETBIT(BYTE,4))
#define NOTA_TEXT(BYTE) (!GETBIT(BYTE,2) && !GETBIT(BYTE,3) && GETBIT(BYTE,4))
#define NOTA_ARRAY(BYTE) (!GETBIT(BYTE,2) && GETBIT(BYTE,3) && !GETBIT(BYTE,4))
#define NOTA_REC 0b00110000
#define NOTA_FLOAT 0b01000000
#define NOTA_INT(BYTE) (GETBIT(BYTE,2) && GETBIT(BYTE,3) && !GETBIT(BYTE,4))
#define NOTA_SYM 0b01110000
#define MASK(n) ((1ULL << n) -1)
#define SMASK(n,s) (~(MASK(n) << s))
#define NEWDATA(d,n,s) (((d) & MASK(n)) << s)
#define SETBITS(d,nd,n,s) (((d) & SMASK(n,s)) | NEWDATA(nd,n,s))
/*
d data
nd new data
n num bits
s startbit
*/
JSValue nota_encode(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) JSValue nota_encode(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
{ {
if (argc < 2) return JS_UNDEFINED; printf("nota encode\n");
if (argc < 1) return JS_UNDEFINED;
JSValue obj = argv[0]; JSValue obj = argv[0];
const char *f = js2str(argv[1]); char nota[1024];
if (JS_IsNumber(obj)) {
int64_t i;
JS_ToInt64(js, &i, obj);
nota_write_int(i, nota);
} else if (JS_IsString(obj)) {
char *str = js2str(obj);
nota_write_text(str, nota);
} else if (JS_IsBool(obj)) {
int b = js2bool(obj);
nota_write_bool(b, nota);
}
return str2js(nota);
} }
JSValue nota_decode(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) JSValue nota_decode(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
{ {
if (argc < 1) return JS_UNDEFINED; if (argc < 1) return JS_UNDEFINED;
size_t len;
char *blob = slurp_file(js2str(argv[0]), &len);
char *byte = blob;
char buf[8]; char *nota = js2str(argv[0]);
int bit = 0; int type = nota_type(nota);
if (!NOTA_INT(*blob)) return JS_UNDEFINED; long long n;
SETBITS(*buf, (*blob)<<3, 3, bit); switch(type) {
byte++; case NOTA_BLOB:
bit +=3; break;
case NOTA_TEXT:
while (GETBIT(*byte, 1)) { return str2js(nota_read_text(nota));
SETBITS(*buf, (*byte)<<7, 7, bit); case NOTA_INT:
bit += 7; printf("type int\n");
nota_read_num(nota, &n);
printf("num is %lld\n", n);
return int2js(n);
case NOTA_SYM:
return bool2js(nota_read_bool(nota));
} }
YughWarn("%#08x", buf);
return JS_UNDEFINED; return JS_UNDEFINED;
} }
void nota_int(char *blob) static const JSCFunctionListEntry nota_funcs[] = {
{ JS_CFUNC_DEF("encode", 1, nota_encode),
char *byte = blob; JS_CFUNC_DEF("decode", 1, nota_decode)
};
char buf[8] = {0};
int bit = 0;
SETBITS(*buf, (*blob)<<3, 3, bit);
byte++;
bit +=3;
while (GETBIT(*byte, 1)) {
SETBITS(*buf, (*byte)<<7, 7, bit);
bit += 7;
}
for (int i = 0; i < 8; i++)
YughWarn("%c", buf[i]);
}
#define DUK_FUNC(NAME, ARGS) JS_SetPropertyStr(js, globalThis, #NAME, JS_NewCFunction(js, duk_##NAME, #NAME, ARGS)); #define DUK_FUNC(NAME, ARGS) JS_SetPropertyStr(js, globalThis, #NAME, JS_NewCFunction(js, duk_##NAME, #NAME, ARGS));
void ffi_load() { void ffi_load() {
globalThis = JS_GetGlobalObject(js); globalThis = JS_GetGlobalObject(js);
DUK_FUNC(yughlog, 4) JSValue nota = JS_NewObject(js);
JS_SetPropertyFunctionList(js, nota, nota_funcs, countof(nota_funcs));
JS_SetPropertyStr(js, globalThis, "nota", nota);
DUK_FUNC(yughlog, 4)
DUK_FUNC(make_gameobject, 0) DUK_FUNC(make_gameobject, 0)
DUK_FUNC(set_body, 3) DUK_FUNC(set_body, 3)
DUK_FUNC(q_body, 2) DUK_FUNC(q_body, 2)
DUK_FUNC(sys_cmd, 1) DUK_FUNC(sys_cmd, 1)
DUK_FUNC(make_sprite, 1) DUK_FUNC(make_sprite, 1)
DUK_FUNC(spline_cmd, 6) DUK_FUNC(spline_cmd, 6)
DUK_FUNC(make_circle2d, 1) DUK_FUNC(make_circle2d, 1)
DUK_FUNC(cmd_circle2d, 6) DUK_FUNC(cmd_circle2d, 6)
DUK_FUNC(make_poly2d, 1) DUK_FUNC(make_poly2d, 1)
@ -2029,9 +2009,7 @@ void ffi_load() {
DUK_FUNC(make_edge2d, 3) DUK_FUNC(make_edge2d, 3)
DUK_FUNC(cmd_edge2d, 6) DUK_FUNC(cmd_edge2d, 6)
DUK_FUNC(make_model,2); DUK_FUNC(make_model,2);
DUK_FUNC(cmd_points, 5); DUK_FUNC(cmd_points, 5);
DUK_FUNC(cmd, 6) DUK_FUNC(cmd, 6)
DUK_FUNC(register, 3) DUK_FUNC(register, 3)
DUK_FUNC(register_collide, 6) DUK_FUNC(register_collide, 6)

View file

@ -7,15 +7,18 @@
#define NOTA_DATA 0x7f #define NOTA_DATA 0x7f
#define NOTA_INT_DATA 0x07 #define NOTA_INT_DATA 0x07
#define NOTA_INT_SIGN(CHAR) (CHAR & (1<<3)) #define NOTA_INT_SIGN(CHAR) (CHAR & (1<<3))
#define NOTA_FLOAT 0x40 #define NOTA_TYPE 0x70
#define NOTA_HEAD_DATA 0x0f #define NOTA_HEAD_DATA 0x0f
#define CONTINUE(CHAR) (CHAR>>7) #define CONTINUE(CHAR) (CHAR>>7)
#define NOTA_BLOB 0x00
#define NOTA_TEXT 0x10 #define NOTA_FALSE 0x00
#define NOTA_REC 0x30 #define NOTA_TRUE 0x01
#define NOTA_ARR 0x20 #define NOTA_PRIVATE 0x08
#define NOTA_INT 0x60 #define NOTA_SYSTEM 0x09
#define NOTA_SYM 0x70
#define UTF8_DATA 0x3f
int nota_type(char *nota) { return *nota & NOTA_TYPE; }
int nota_bits(long long n, int sb) int nota_bits(long long n, int sb)
{ {
@ -41,7 +44,7 @@ char *nota_continue_num(long long n, char *nota, int sb)
while (bits > 0) { while (bits > 0) {
bits -= 7; bits -= 7;
int head = bits == 0 ? 0 : NOTA_CONT; int head = bits == 0 ? 0 : NOTA_CONT;
nota[i] = head | (0x7f & (n >> bits)); nota[i] = head | (NOTA_DATA & (n >> bits));
i++; i++;
} }
@ -49,27 +52,26 @@ char *nota_continue_num(long long n, char *nota, int sb)
} }
void print_nota(char *nota) void print_nota_hex(char *nota)
{ {
int chars = 0; long long chars = 0;
if (!(nota[0]>>4 ^ NOTA_TEXT>>4)) { if (!((*nota>>4 & 0x07) ^ NOTA_TEXT>>4)) {
chars = nota_read_int(nota); nota_read_num(nota, &chars);
printf("there are %d chars in the text\n", chars); printf("print with %d points\n", chars);
} }
char *c = nota;
do {
printf("%02X ", (unsigned char)(*c));
} while(CONTINUE(*(c++)));
for (int i = 0; i < chars; i++) for (int i = 0; i < chars+1; i++) {
printf("%02X ", (unsigned char)c[i]); do {
printf("%02X ", (unsigned char)(*nota));
} while(CONTINUE(*(nota++)));
}
printf("\n"); printf("\n");
} }
void nota_write_int(long long n, char *nota) void nota_write_int(long long n, char *nota)
{ {
printf("number %ld\n", n); printf("number %lld\n", n);
char sign = 0; char sign = 0;
if (n < 0) { if (n < 0) {
@ -80,19 +82,18 @@ void nota_write_int(long long n, char *nota)
nota[0] = NOTA_INT | sign; nota[0] = NOTA_INT | sign;
nota_continue_num(n, nota, 3); nota_continue_num(n, nota, 3);
print_nota(nota); print_nota_hex(nota);
} }
long long nota_read_num(char *nota) char *nota_read_num(char *nota, long long *n)
{ {
long long n = 0; *n = 0;
char *c = nota; *n |= (*nota) & NOTA_HEAD_DATA;
n |= (*c) & NOTA_HEAD_DATA;
while (CONTINUE(*(c++))) while (CONTINUE(*(nota++)))
n = (n<<7) | (*c) & NOTA_DATA; *n = (*n<<7) | (*nota) & NOTA_DATA;
return n; return nota;
} }
void nota_write_float(double n, char *nota) void nota_write_float(double n, char *nota)
@ -103,6 +104,7 @@ void nota_write_float(double n, char *nota)
double nota_read_float(char *nota) double nota_read_float(char *nota)
{ {
} }
long long nota_read_int(char *nota) long long nota_read_int(char *nota)
@ -120,26 +122,153 @@ long long nota_read_int(char *nota)
/* n is the number of bits */ /* n is the number of bits */
void nota_write_blob(unsigned long long n, char *nota) void nota_write_blob(unsigned long long n, char *nota)
{ {
printf("blob %ld\n", n); printf("blob %lld\n", n);
nota[0] = NOTA_BLOB; nota[0] = NOTA_BLOB;
nota_continue_num(n, nota, 4); nota_continue_num(n, nota, 4);
print_nota(nota); print_nota_hex(nota);
} }
void nota_write_array(unsigned long long n, char *nota) void nota_write_array(unsigned long long n, char *nota)
{ {
printf("array %ld\n", n); printf("array %lld\n", n);
nota[0] = NOTA_ARR; nota[0] = NOTA_ARR;
nota_continue_num(n, nota, 4); nota_continue_num(n, nota, 4);
print_nota(nota); print_nota_hex(nota);
}
/* kim is 7, 14, then 21 */
int utf8_bytes(char *s)
{
int bytes = __builtin_clz(~(*s));
if (!bytes) return 1;
return bytes-24;
}
int utf8_count(char *s)
{
int count = 0;
char *p = s;
while(*s) {
count++;
s += utf8_bytes(s);
}
return count;
}
int decode_utf8(char **s) {
int k = **s ? __builtin_clz(~(**s << 24)) : 0; // Count # of leading 1 bits.
int mask = (1 << (8 - k)) - 1; // All 1's with k leading 0's.
int value = **s & mask;
for (++(*s), --k; k > 0 && **s; --k, ++(*s)) { // Note that k = #total bytes, or 0.
value <<= 6;
value += (**s & 0x3F);
}
return value;
}
void encode_utf8(char **s, char *end, int code) {
if (code < 255) {
**s = code;
(*s)++;
return;
}
char val[4];
int lead_byte_max = 0x7F;
int val_index = 0;
while (code > lead_byte_max) {
val[val_index++] = (code & 0x3F) | 0x80;
code >>= 6;
lead_byte_max >>= (val_index == 1 ? 2 : 1);
}
val[val_index++] = (code & lead_byte_max) | (~lead_byte_max << 1);
while (val_index-- && *s < end) {
**s = val[val_index];
(*s)++;
}
}
void encode_kim(char **s, char *end, int code)
{
if (code < 255) {
**s = 0 | (NOTA_DATA & code);
(*s)++;
return;
}
int bits = 32 - __builtin_clz(code);
if (bits <= 7)
bits = 7;
else if (bits <= 14)
bits = 14;
else
bits = 21;
while (bits > 7) {
bits -= 7;
**s = NOTA_CONT | NOTA_DATA & (code >> bits);
(*s)++;
}
**s = NOTA_DATA & code;
(*s)++;
}
int decode_kim(char **s)
{
int rune = **s & NOTA_DATA;
while (CONTINUE(**s)) {
rune <<= 7;
(*s)++;
rune |= **s & NOTA_DATA;
}
(*s)++;
return rune;
}
void utf8_to_kim(char *utf, char *kim)
{
while (*utf)
encode_kim(&kim, NULL, decode_utf8(&utf));
}
void kim_to_utf8(char *kim, char *utf, int runes)
{
for (int i = 0; i < runes; i++)
encode_utf8(&utf, utf+4, decode_kim(&kim));
*utf = 0;
}
char *nota_read_text(char *nota)
{
long long chars;
nota = nota_read_num(nota, &chars);
printf("reading %d runes\n", chars);
char utf[chars*4];
kim_to_utf8(nota, utf, chars);
return strdup(utf);
}
void nota_write_bool(int b, char *nota)
{
*nota = NOTA_SYM | (b ? NOTA_TRUE : NOTA_FALSE);
}
int nota_read_bool(char *nota)
{
return *nota & 0x0f;
} }
void nota_write_text(char *s, char *nota) void nota_write_text(char *s, char *nota)
{ {
printf("text %s\n", s); char *start = nota;
nota[0] = NOTA_TEXT; nota[0] = NOTA_TEXT;
long n = strlen(s); long n = utf8_count(s);
char *start = nota_continue_num(n, nota, 4); printf("text %s with %d points\n", s, n);
memcpy(start, s, n); nota = nota_continue_num(n,nota,4);
print_nota(nota); utf8_to_kim(s, nota);
print_nota_hex(start);
} }

View file

@ -1,12 +1,24 @@
#ifndef NOTA_H #ifndef NOTA_H
#define NOTA_H #define NOTA_H
#define NOTA_BLOB 0x00
#define NOTA_TEXT 0x10
#define NOTA_ARR 0x20
#define NOTA_REC 0x30
#define NOTA_FLOAT 0x40
#define NOTA_INT 0x60
#define NOTA_SYM 0x70
int nota_type(char *nota);
void nota_write_int(long long n, char *nota); void nota_write_int(long long n, char *nota);
long long nota_read_int(char *nota); long long nota_read_int(char *nota);
long long nota_read_num(char *nota); char *nota_read_num(char *nota, long long *n);
double nota_read_float(char *nota); double nota_read_float(char *nota);
void nota_write_bool(int b, char *nota);
int nota_read_bool(char *nota);
void nota_write_blob(unsigned long long n, char *nota); void nota_write_blob(unsigned long long n, char *nota);
void nota_write_array(unsigned long long n, char *nota); void nota_write_array(unsigned long long n, char *nota);
void nota_write_text(char *s, char *nota); void nota_write_text(char *s, char *nota);
char *nota_read_text(char *nota);
#endif #endif

View file

@ -11,6 +11,7 @@
#include <stdio.h> #include <stdio.h>
#include "particle.h" #include "particle.h"
#include "simplex.h" #include "simplex.h"
#include "nota.h"
#include "datastream.h" #include "datastream.h"
@ -283,9 +284,15 @@ int main(int argc, char **argv) {
#endif #endif
/* char nota[1024];
nota_int("\xe3\x74"); nota_write_int(1234, nota);
nota_write_text("cat", nota);
nota_write_text("☃★♲", nota);
nota_write_text("𓂀𓃠𓅣𓂻𓂻𓂺𓁟𓂑𓃻𓇼𓊽𓂭𓎆𓍢𓏢𓐠", nota);
nota_write_text("test kim ☃★♲", nota);
char *re = nota_read_text(nota);
printf("%s\n", re);
*/
#ifdef STEAM #ifdef STEAM
steaminit(); steaminit();
#endif #endif