Compare commits
3 Commits
3c38e828e5
...
cellfix
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
970386a04b | ||
|
|
75ddb1fb4f | ||
|
|
bc9d8a338a |
394
internal/nota.c
Executable file
394
internal/nota.c
Executable file
@@ -0,0 +1,394 @@
|
||||
#include "cell.h"
|
||||
#include "cell_internal.h"
|
||||
|
||||
#define NOTA_IMPLEMENTATION
|
||||
#include "nota.h"
|
||||
|
||||
typedef struct NotaEncodeContext {
|
||||
JSContext *ctx;
|
||||
JSValue visitedStack;
|
||||
NotaBuffer nb;
|
||||
int cycle;
|
||||
JSValue replacer;
|
||||
} NotaEncodeContext;
|
||||
|
||||
static void nota_stack_push(NotaEncodeContext *enc, JSValueConst val)
|
||||
{
|
||||
JSContext *ctx = enc->ctx;
|
||||
int len = JS_ArrayLength(ctx, enc->visitedStack);
|
||||
JS_SetPropertyInt64(ctx, enc->visitedStack, len, JS_DupValue(ctx, val));
|
||||
}
|
||||
|
||||
static void nota_stack_pop(NotaEncodeContext *enc)
|
||||
{
|
||||
JSContext *ctx = enc->ctx;
|
||||
int len = JS_ArrayLength(ctx, enc->visitedStack);
|
||||
JS_SetPropertyStr(ctx, enc->visitedStack, "length", JS_NewUint32(ctx, len - 1));
|
||||
}
|
||||
|
||||
static int nota_stack_has(NotaEncodeContext *enc, JSValueConst val)
|
||||
{
|
||||
JSContext *ctx = enc->ctx;
|
||||
int len = JS_ArrayLength(ctx, enc->visitedStack);
|
||||
for (int i = 0; i < len; i++) {
|
||||
JSValue elem = JS_GetPropertyUint32(ctx, enc->visitedStack, i);
|
||||
if (JS_IsObject(elem) && JS_IsObject(val)) {
|
||||
if (JS_StrictEq(ctx, elem, val)) {
|
||||
JS_FreeValue(ctx, elem);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
JS_FreeValue(ctx, elem);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static JSValue apply_replacer(NotaEncodeContext *enc, JSValueConst holder, JSValueConst key, JSValueConst val) {
|
||||
if (JS_IsNull(enc->replacer)) return JS_DupValue(enc->ctx, val);
|
||||
|
||||
JSValue args[2] = { JS_DupValue(enc->ctx, key), JS_DupValue(enc->ctx, val) };
|
||||
JSValue result = JS_Call(enc->ctx, enc->replacer, holder, 2, args);
|
||||
JS_FreeValue(enc->ctx, args[0]);
|
||||
JS_FreeValue(enc->ctx, args[1]);
|
||||
|
||||
if (JS_IsException(result)) return JS_DupValue(enc->ctx, val);
|
||||
return result;
|
||||
}
|
||||
|
||||
char *js_do_nota_decode(JSContext *js, JSValue *tmp, char *nota, JSValue holder, JSValue key, JSValue reviver) {
|
||||
int type = nota_type(nota);
|
||||
JSValue ret2;
|
||||
long long n;
|
||||
double d;
|
||||
int b;
|
||||
char *str;
|
||||
uint8_t *blob;
|
||||
|
||||
switch(type) {
|
||||
case NOTA_BLOB:
|
||||
nota = nota_read_blob(&n, (char**)&blob, nota);
|
||||
*tmp = js_new_blob_stoned_copy(js, blob, n);
|
||||
free(blob);
|
||||
break;
|
||||
case NOTA_TEXT:
|
||||
nota = nota_read_text(&str, nota);
|
||||
*tmp = JS_NewString(js, str);
|
||||
free(str);
|
||||
break;
|
||||
case NOTA_ARR:
|
||||
nota = nota_read_array(&n, nota);
|
||||
*tmp = JS_NewArray(js);
|
||||
for (int i = 0; i < n; i++) {
|
||||
nota = js_do_nota_decode(js, &ret2, nota, *tmp, JS_NewInt32(js, i), reviver);
|
||||
JS_SetPropertyInt64(js, *tmp, i, ret2);
|
||||
}
|
||||
break;
|
||||
case NOTA_REC:
|
||||
nota = nota_read_record(&n, nota);
|
||||
*tmp = JS_NewObject(js);
|
||||
for (int i = 0; i < n; i++) {
|
||||
nota = nota_read_text(&str, nota);
|
||||
JSValue prop_key = JS_NewString(js, str);
|
||||
nota = js_do_nota_decode(js, &ret2, nota, *tmp, prop_key, reviver);
|
||||
JS_SetPropertyStr(js, *tmp, str, ret2);
|
||||
JS_FreeValue(js, prop_key);
|
||||
free(str);
|
||||
}
|
||||
break;
|
||||
case NOTA_INT:
|
||||
nota = nota_read_int(&n, nota);
|
||||
*tmp = JS_NewInt64(js, n);
|
||||
break;
|
||||
case NOTA_SYM:
|
||||
nota = nota_read_sym(&b, nota);
|
||||
if (b == NOTA_PRIVATE) {
|
||||
JSValue inner;
|
||||
nota = js_do_nota_decode(js, &inner, nota, holder, JS_NULL, reviver);
|
||||
JSValue obj = JS_NewObject(js);
|
||||
cell_rt *crt = JS_GetContextOpaque(js);
|
||||
// JS_SetProperty(js, obj, crt->actor_sym, inner);
|
||||
*tmp = obj;
|
||||
} else {
|
||||
switch(b) {
|
||||
case NOTA_NULL: *tmp = JS_NULL; break;
|
||||
case NOTA_FALSE: *tmp = JS_NewBool(js, 0); break;
|
||||
case NOTA_TRUE: *tmp = JS_NewBool(js, 1); break;
|
||||
default: *tmp = JS_NULL; break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
case NOTA_FLOAT:
|
||||
nota = nota_read_float(&d, nota);
|
||||
*tmp = JS_NewFloat64(js, d);
|
||||
break;
|
||||
}
|
||||
|
||||
if (!JS_IsNull(reviver)) {
|
||||
JSValue args[2] = { JS_DupValue(js, key), JS_DupValue(js, *tmp) };
|
||||
JSValue revived = JS_Call(js, reviver, holder, 2, args);
|
||||
JS_FreeValue(js, args[0]);
|
||||
JS_FreeValue(js, args[1]);
|
||||
if (!JS_IsException(revived)) {
|
||||
JS_FreeValue(js, *tmp);
|
||||
*tmp = revived;
|
||||
} else {
|
||||
JS_FreeValue(js, revived);
|
||||
}
|
||||
}
|
||||
|
||||
return nota;
|
||||
}
|
||||
|
||||
static void nota_encode_value(NotaEncodeContext *enc, JSValueConst val, JSValueConst holder, JSValueConst key) {
|
||||
JSContext *ctx = enc->ctx;
|
||||
JSValue replaced = apply_replacer(enc, holder, key, val);
|
||||
int tag = JS_VALUE_GET_TAG(replaced);
|
||||
|
||||
switch (tag) {
|
||||
case JS_TAG_INT:
|
||||
case JS_TAG_FLOAT64: {
|
||||
double d;
|
||||
JS_ToFloat64(ctx, &d, replaced);
|
||||
nota_write_number(&enc->nb, d);
|
||||
break;
|
||||
}
|
||||
case JS_TAG_STRING: {
|
||||
const char *str = JS_ToCString(ctx, replaced);
|
||||
nota_write_text(&enc->nb, str);
|
||||
JS_FreeCString(ctx, str);
|
||||
break;
|
||||
}
|
||||
case JS_TAG_BOOL:
|
||||
if (JS_VALUE_GET_BOOL(replaced)) nota_write_sym(&enc->nb, NOTA_TRUE);
|
||||
else nota_write_sym(&enc->nb, NOTA_FALSE);
|
||||
break;
|
||||
case JS_TAG_NULL:
|
||||
nota_write_sym(&enc->nb, NOTA_NULL);
|
||||
break;
|
||||
case JS_TAG_PTR: {
|
||||
if (js_is_blob(ctx, replaced)) {
|
||||
size_t buf_len;
|
||||
void *buf_data = js_get_blob_data(ctx, &buf_len, replaced);
|
||||
if (buf_data == -1) {
|
||||
JS_FreeValue(ctx, replaced);
|
||||
return; // JS_EXCEPTION will be handled by caller
|
||||
}
|
||||
nota_write_blob(&enc->nb, (unsigned long long)buf_len * 8, (const char*)buf_data);
|
||||
break;
|
||||
}
|
||||
|
||||
if (JS_IsArray(replaced)) {
|
||||
if (nota_stack_has(enc, replaced)) {
|
||||
enc->cycle = 1;
|
||||
break;
|
||||
}
|
||||
nota_stack_push(enc, replaced);
|
||||
int arr_len = JS_ArrayLength(ctx, replaced);
|
||||
nota_write_array(&enc->nb, arr_len);
|
||||
for (int i = 0; i < arr_len; i++) {
|
||||
JSValue elem_val = JS_GetPropertyUint32(ctx, replaced, i);
|
||||
JSValue elem_key = JS_NewInt32(ctx, i);
|
||||
nota_encode_value(enc, elem_val, replaced, elem_key);
|
||||
JS_FreeValue(ctx, elem_val);
|
||||
JS_FreeValue(ctx, elem_key);
|
||||
}
|
||||
nota_stack_pop(enc);
|
||||
break;
|
||||
}
|
||||
|
||||
cell_rt *crt = JS_GetContextOpaque(ctx);
|
||||
// JSValue adata = JS_GetProperty(ctx, replaced, crt->actor_sym);
|
||||
JSValue adata = JS_NULL;
|
||||
if (!JS_IsNull(adata)) {
|
||||
nota_write_sym(&enc->nb, NOTA_PRIVATE);
|
||||
nota_encode_value(enc, adata, replaced, JS_NULL);
|
||||
JS_FreeValue(ctx, adata);
|
||||
break;
|
||||
}
|
||||
JS_FreeValue(ctx, adata);
|
||||
if (nota_stack_has(enc, replaced)) {
|
||||
enc->cycle = 1;
|
||||
break;
|
||||
}
|
||||
nota_stack_push(enc, replaced);
|
||||
|
||||
JSValue to_json = JS_GetPropertyStr(ctx, replaced, "toJSON");
|
||||
if (JS_IsFunction(to_json)) {
|
||||
JSValue result = JS_Call(ctx, to_json, replaced, 0, NULL);
|
||||
JS_FreeValue(ctx, to_json);
|
||||
if (!JS_IsException(result)) {
|
||||
nota_encode_value(enc, result, holder, key);
|
||||
JS_FreeValue(ctx, result);
|
||||
} else {
|
||||
nota_write_sym(&enc->nb, NOTA_NULL);
|
||||
}
|
||||
nota_stack_pop(enc);
|
||||
break;
|
||||
}
|
||||
JS_FreeValue(ctx, to_json);
|
||||
|
||||
JSValue keys = JS_GetOwnPropertyNames(ctx, replaced);
|
||||
if (JS_IsException(keys)) {
|
||||
nota_write_sym(&enc->nb, NOTA_NULL);
|
||||
nota_stack_pop(enc);
|
||||
break;
|
||||
}
|
||||
int64_t plen64;
|
||||
if (JS_GetLength(ctx, keys, &plen64) < 0) {
|
||||
JS_FreeValue(ctx, keys);
|
||||
nota_write_sym(&enc->nb, NOTA_NULL);
|
||||
nota_stack_pop(enc);
|
||||
break;
|
||||
}
|
||||
uint32_t plen = (uint32_t)plen64;
|
||||
|
||||
uint32_t non_function_count = 0;
|
||||
for (uint32_t i = 0; i < plen; i++) {
|
||||
JSValue key = JS_GetPropertyUint32(ctx, keys, i);
|
||||
JSValue prop_val = JS_GetProperty(ctx, replaced, key);
|
||||
if (!JS_IsFunction(prop_val)) non_function_count++;
|
||||
JS_FreeValue(ctx, prop_val);
|
||||
JS_FreeValue(ctx, key);
|
||||
}
|
||||
|
||||
nota_write_record(&enc->nb, non_function_count);
|
||||
for (uint32_t i = 0; i < plen; i++) {
|
||||
JSValue key = JS_GetPropertyUint32(ctx, keys, i);
|
||||
JSValue prop_val = JS_GetProperty(ctx, replaced, key);
|
||||
if (!JS_IsFunction(prop_val)) {
|
||||
const char *prop_name = JS_ToCString(ctx, key);
|
||||
nota_write_text(&enc->nb, prop_name ? prop_name : "");
|
||||
nota_encode_value(enc, prop_val, replaced, key);
|
||||
JS_FreeCString(ctx, prop_name);
|
||||
}
|
||||
JS_FreeValue(ctx, prop_val);
|
||||
JS_FreeValue(ctx, key);
|
||||
}
|
||||
JS_FreeValue(ctx, keys);
|
||||
nota_stack_pop(enc);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
nota_write_sym(&enc->nb, NOTA_NULL);
|
||||
break;
|
||||
}
|
||||
JS_FreeValue(ctx, replaced);
|
||||
}
|
||||
|
||||
void *value2nota(JSContext *ctx, JSValue v) {
|
||||
NotaEncodeContext enc_s, *enc = &enc_s;
|
||||
enc->ctx = ctx;
|
||||
enc->visitedStack = JS_NewArray(ctx);
|
||||
enc->cycle = 0;
|
||||
enc->replacer = JS_NULL;
|
||||
|
||||
nota_buffer_init(&enc->nb, 128);
|
||||
nota_encode_value(enc, v, JS_NULL, JS_NewString(ctx, ""));
|
||||
|
||||
if (enc->cycle) {
|
||||
JS_FreeValue(ctx, enc->visitedStack);
|
||||
nota_buffer_free(&enc->nb);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
JS_FreeValue(ctx, enc->visitedStack);
|
||||
void *data_ptr = enc->nb.data;
|
||||
enc->nb.data = NULL;
|
||||
nota_buffer_free(&enc->nb);
|
||||
return data_ptr;
|
||||
}
|
||||
|
||||
JSValue nota2value(JSContext *js, void *nota) {
|
||||
if (!nota) return JS_NULL;
|
||||
JSValue ret;
|
||||
JSValue holder = JS_NewObject(js);
|
||||
js_do_nota_decode(js, &ret, nota, holder, JS_NewString(js, ""), JS_NULL);
|
||||
JS_FreeValue(js, holder);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static JSValue js_nota_tostring(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
|
||||
size_t len;
|
||||
void *nota = js_get_blob_data(ctx, &len, this_val);
|
||||
if (nota == (void*)-1) return JS_EXCEPTION;
|
||||
if (!nota) return JS_NULL;
|
||||
|
||||
JSValue decoded;
|
||||
JSValue holder = JS_NewObject(ctx);
|
||||
js_do_nota_decode(ctx, &decoded, (char*)nota, holder, JS_NewString(ctx, ""), JS_NULL);
|
||||
JS_FreeValue(ctx, holder);
|
||||
|
||||
JSValue global = JS_GetGlobalObject(ctx);
|
||||
JSValue json = JS_GetPropertyStr(ctx, global, "JSON");
|
||||
JSValue stringify = JS_GetPropertyStr(ctx, json, "stringify");
|
||||
|
||||
JSValue args[3];
|
||||
args[0] = decoded;
|
||||
args[1] = JS_NULL;
|
||||
args[2] = JS_NewInt32(ctx, 1);
|
||||
|
||||
JSValue result = JS_Call(ctx, stringify, json, 3, args);
|
||||
|
||||
JS_FreeValue(ctx, stringify);
|
||||
JS_FreeValue(ctx, json);
|
||||
JS_FreeValue(ctx, global);
|
||||
JS_FreeValue(ctx, decoded);
|
||||
JS_FreeValue(ctx, args[2]);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static JSValue js_nota_encode(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
|
||||
if (argc < 1) return JS_ThrowTypeError(ctx, "nota.encode requires at least 1 argument");
|
||||
|
||||
NotaEncodeContext enc_s, *enc = &enc_s;
|
||||
enc->ctx = ctx;
|
||||
enc->visitedStack = JS_NewArray(ctx);
|
||||
enc->cycle = 0;
|
||||
enc->replacer = (argc > 1 && JS_IsFunction(argv[1])) ? argv[1] : JS_NULL;
|
||||
|
||||
nota_buffer_init(&enc->nb, 128);
|
||||
nota_encode_value(enc, argv[0], JS_NULL, JS_NewString(ctx, ""));
|
||||
|
||||
if (enc->cycle) {
|
||||
JS_FreeValue(ctx, enc->visitedStack);
|
||||
nota_buffer_free(&enc->nb);
|
||||
return JS_ThrowReferenceError(ctx, "Tried to encode something to nota with a cycle.");
|
||||
}
|
||||
|
||||
JS_FreeValue(ctx, enc->visitedStack);
|
||||
size_t total_len = enc->nb.size;
|
||||
void *data_ptr = enc->nb.data;
|
||||
JSValue ret = js_new_blob_stoned_copy(ctx, (uint8_t*)data_ptr, total_len);
|
||||
|
||||
nota_buffer_free(&enc->nb);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static JSValue js_nota_decode(JSContext *js, JSValueConst self, int argc, JSValueConst *argv) {
|
||||
if (argc < 1) return JS_NULL;
|
||||
|
||||
size_t len;
|
||||
unsigned char *nota = js_get_blob_data(js, &len, argv[0]);
|
||||
if (nota == -1) return JS_EXCEPTION;
|
||||
if (!nota) return JS_NULL;
|
||||
|
||||
JSValue reviver = (argc > 1 && JS_IsFunction(argv[1])) ? argv[1] : JS_NULL;
|
||||
JSValue ret;
|
||||
JSValue holder = JS_NewObject(js);
|
||||
js_do_nota_decode(js, &ret, (char*)nota, holder, JS_NewString(js, ""), reviver);
|
||||
JS_FreeValue(js, holder);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static const JSCFunctionListEntry js_nota_funcs[] = {
|
||||
JS_CFUNC_DEF("encode", 1, js_nota_encode),
|
||||
JS_CFUNC_DEF("decode", 1, js_nota_decode),
|
||||
};
|
||||
|
||||
JSValue js_nota_use(JSContext *js) {
|
||||
JSValue export = JS_NewObject(js);
|
||||
JS_SetPropertyFunctionList(js, export, js_nota_funcs, sizeof(js_nota_funcs)/sizeof(JSCFunctionListEntry));
|
||||
return export;
|
||||
}
|
||||
@@ -43,6 +43,7 @@ src += [ # core
|
||||
'suite.c',
|
||||
'wildmatch.c',
|
||||
'qjs_actor.c',
|
||||
'qjs_wota.c',
|
||||
'miniz.c',
|
||||
'quickjs.c',
|
||||
'libregexp.c', 'libunicode.c', 'cutils.c', 'dtoa.c'
|
||||
@@ -51,6 +52,7 @@ src += [ # core
|
||||
src += ['scheduler.c']
|
||||
|
||||
scripts = [
|
||||
'internal/nota.c',
|
||||
'debug/js.c',
|
||||
'qop.c',
|
||||
'wildstar.c',
|
||||
@@ -58,6 +60,7 @@ scripts = [
|
||||
'crypto.c',
|
||||
'internal/kim.c',
|
||||
'time.c',
|
||||
'internal/nota.c',
|
||||
'debug/debug.c',
|
||||
'internal/os.c',
|
||||
'fd.c',
|
||||
@@ -65,7 +68,6 @@ scripts = [
|
||||
'net/enet.c',
|
||||
'wildstar.c',
|
||||
'archive/miniz.c',
|
||||
'source/cJSON.c'
|
||||
]
|
||||
|
||||
foreach file: scripts
|
||||
|
||||
3191
source/cJSON.c
3191
source/cJSON.c
File diff suppressed because it is too large
Load Diff
306
source/cJSON.h
306
source/cJSON.h
@@ -1,306 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef cJSON__h
|
||||
#define cJSON__h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
|
||||
#define __WINDOWS__
|
||||
#endif
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
|
||||
/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 3 define options:
|
||||
|
||||
CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols
|
||||
CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default)
|
||||
CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol
|
||||
|
||||
For *nix builds that support visibility attribute, you can define similar behavior by
|
||||
|
||||
setting default visibility to hidden by adding
|
||||
-fvisibility=hidden (for gcc)
|
||||
or
|
||||
-xldscope=hidden (for sun cc)
|
||||
to CFLAGS
|
||||
|
||||
then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does
|
||||
|
||||
*/
|
||||
|
||||
#define CJSON_CDECL __cdecl
|
||||
#define CJSON_STDCALL __stdcall
|
||||
|
||||
/* export symbols by default, this is necessary for copy pasting the C and header file */
|
||||
#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS)
|
||||
#define CJSON_EXPORT_SYMBOLS
|
||||
#endif
|
||||
|
||||
#if defined(CJSON_HIDE_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) type CJSON_STDCALL
|
||||
#elif defined(CJSON_EXPORT_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL
|
||||
#elif defined(CJSON_IMPORT_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL
|
||||
#endif
|
||||
#else /* !__WINDOWS__ */
|
||||
#define CJSON_CDECL
|
||||
#define CJSON_STDCALL
|
||||
|
||||
#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY)
|
||||
#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type
|
||||
#else
|
||||
#define CJSON_PUBLIC(type) type
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* project version */
|
||||
#define CJSON_VERSION_MAJOR 1
|
||||
#define CJSON_VERSION_MINOR 7
|
||||
#define CJSON_VERSION_PATCH 19
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/* cJSON Types: */
|
||||
#define cJSON_Invalid (0)
|
||||
#define cJSON_False (1 << 0)
|
||||
#define cJSON_True (1 << 1)
|
||||
#define cJSON_NULL (1 << 2)
|
||||
#define cJSON_Number (1 << 3)
|
||||
#define cJSON_String (1 << 4)
|
||||
#define cJSON_Array (1 << 5)
|
||||
#define cJSON_Object (1 << 6)
|
||||
#define cJSON_Raw (1 << 7) /* raw json */
|
||||
|
||||
#define cJSON_IsReference 256
|
||||
#define cJSON_StringIsConst 512
|
||||
|
||||
/* The cJSON structure: */
|
||||
typedef struct cJSON
|
||||
{
|
||||
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
|
||||
struct cJSON *next;
|
||||
struct cJSON *prev;
|
||||
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
|
||||
struct cJSON *child;
|
||||
|
||||
/* The type of the item, as above. */
|
||||
int type;
|
||||
|
||||
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
|
||||
char *valuestring;
|
||||
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
|
||||
int valueint;
|
||||
/* The item's number, if type==cJSON_Number */
|
||||
double valuedouble;
|
||||
|
||||
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
|
||||
char *string;
|
||||
} cJSON;
|
||||
|
||||
typedef struct cJSON_Hooks
|
||||
{
|
||||
/* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */
|
||||
void *(CJSON_CDECL *malloc_fn)(size_t sz);
|
||||
void (CJSON_CDECL *free_fn)(void *ptr);
|
||||
} cJSON_Hooks;
|
||||
|
||||
typedef int cJSON_bool;
|
||||
|
||||
/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them.
|
||||
* This is to prevent stack overflows. */
|
||||
#ifndef CJSON_NESTING_LIMIT
|
||||
#define CJSON_NESTING_LIMIT 1000
|
||||
#endif
|
||||
|
||||
/* Limits the length of circular references can be before cJSON rejects to parse them.
|
||||
* This is to prevent stack overflows. */
|
||||
#ifndef CJSON_CIRCULAR_LIMIT
|
||||
#define CJSON_CIRCULAR_LIMIT 10000
|
||||
#endif
|
||||
|
||||
/* returns the version of cJSON as a string */
|
||||
CJSON_PUBLIC(const char*) cJSON_Version(void);
|
||||
|
||||
/* Supply malloc, realloc and free functions to cJSON */
|
||||
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
|
||||
|
||||
/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
|
||||
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length);
|
||||
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
|
||||
/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated);
|
||||
|
||||
/* Render a cJSON entity to text for transfer/storage. */
|
||||
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
|
||||
/* Render a cJSON entity to text for transfer/storage without any formatting. */
|
||||
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
|
||||
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
|
||||
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
|
||||
/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
|
||||
/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format);
|
||||
/* Delete a cJSON entity and all subentities. */
|
||||
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item);
|
||||
|
||||
/* Returns the number of items in an array (or object). */
|
||||
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
|
||||
/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
|
||||
/* Get item "string" from object. Case insensitive. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
|
||||
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
|
||||
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
|
||||
|
||||
/* Check item type and return its value */
|
||||
CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item);
|
||||
CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item);
|
||||
|
||||
/* These functions check the type of an item */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
|
||||
|
||||
/* These calls create a cJSON item of the appropriate type. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
|
||||
/* raw json */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
|
||||
|
||||
/* Create a string where valuestring references a string so
|
||||
* it will not be freed by cJSON_Delete */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string);
|
||||
/* Create an object/array that only references it's elements so
|
||||
* they will not be freed by cJSON_Delete */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child);
|
||||
|
||||
/* These utilities create an Array of count items.
|
||||
* The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count);
|
||||
|
||||
/* Append item to the specified array/object. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
|
||||
/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object.
|
||||
* WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before
|
||||
* writing to `item->string` */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
|
||||
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
|
||||
|
||||
/* Remove/Detach items from Arrays/Objects. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
|
||||
|
||||
/* Update array items. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem);
|
||||
|
||||
/* Duplicate a cJSON item */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
|
||||
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
|
||||
* need to be released. With recurse!=0, it will duplicate any children connected to the item.
|
||||
* The item->next and ->prev pointers are always zero on return from Duplicate. */
|
||||
/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal.
|
||||
* case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive);
|
||||
|
||||
/* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings.
|
||||
* The input pointer json cannot point to a read-only address area, such as a string constant,
|
||||
* but should point to a readable and writable address area. */
|
||||
CJSON_PUBLIC(void) cJSON_Minify(char *json);
|
||||
|
||||
/* Helper functions for creating and adding items to an object at the same time.
|
||||
* They return the added item or NULL on failure. */
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name);
|
||||
|
||||
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
|
||||
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
|
||||
/* helper for the cJSON_SetNumberValue macro */
|
||||
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
|
||||
#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
|
||||
/* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */
|
||||
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring);
|
||||
|
||||
/* If the object is not a boolean type this does nothing and returns cJSON_Invalid else it returns the new type*/
|
||||
#define cJSON_SetBoolValue(object, boolValue) ( \
|
||||
(object != NULL && ((object)->type & (cJSON_False|cJSON_True))) ? \
|
||||
(object)->type=((object)->type &(~(cJSON_False|cJSON_True)))|((boolValue)?cJSON_True:cJSON_False) : \
|
||||
cJSON_Invalid\
|
||||
)
|
||||
|
||||
/* Macro for iterating over an array or object */
|
||||
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
|
||||
|
||||
/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */
|
||||
CJSON_PUBLIC(void *) cJSON_malloc(size_t size);
|
||||
CJSON_PUBLIC(void) cJSON_free(void *object);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
462
source/cell.c
462
source/cell.c
@@ -2,6 +2,7 @@
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#define WOTA_IMPLEMENTATION
|
||||
#include "wota.h"
|
||||
|
||||
#define STB_DS_IMPLEMENTATION
|
||||
@@ -9,13 +10,11 @@
|
||||
|
||||
#include "cell.h"
|
||||
#include "cell_internal.h"
|
||||
#include "cJSON.h"
|
||||
|
||||
#define ENGINE "internal/engine.cm"
|
||||
#define CELL_SHOP_DIR ".cell"
|
||||
#define CELL_CORE_DIR "packages/core"
|
||||
|
||||
#include <math.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
@@ -105,35 +104,6 @@ static char* load_core_file(const char *filename, size_t *out_size) {
|
||||
return data;
|
||||
}
|
||||
|
||||
static int print_json_errors(const char *json) {
|
||||
if (!json) return 0;
|
||||
cJSON *root = cJSON_Parse(json);
|
||||
if (!root) return 0;
|
||||
cJSON *errors = cJSON_GetObjectItemCaseSensitive(root, "errors");
|
||||
if (!cJSON_IsArray(errors) || cJSON_GetArraySize(errors) == 0) {
|
||||
cJSON_Delete(root);
|
||||
return 0;
|
||||
}
|
||||
const char *filename = "<unknown>";
|
||||
cJSON *fname = cJSON_GetObjectItemCaseSensitive(root, "filename");
|
||||
if (cJSON_IsString(fname))
|
||||
filename = fname->valuestring;
|
||||
cJSON *e;
|
||||
cJSON_ArrayForEach(e, errors) {
|
||||
const char *msg = cJSON_GetStringValue(
|
||||
cJSON_GetObjectItemCaseSensitive(e, "message"));
|
||||
cJSON *line = cJSON_GetObjectItemCaseSensitive(e, "line");
|
||||
cJSON *col = cJSON_GetObjectItemCaseSensitive(e, "column");
|
||||
if (msg && cJSON_IsNumber(line) && cJSON_IsNumber(col))
|
||||
fprintf(stderr, "%s:%d:%d: error: %s\n",
|
||||
filename, (int)line->valuedouble, (int)col->valuedouble, msg);
|
||||
else if (msg)
|
||||
fprintf(stderr, "%s: error: %s\n", filename, msg);
|
||||
}
|
||||
cJSON_Delete(root);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Get the core path for use by scripts
|
||||
const char* cell_get_core_path(void) {
|
||||
return core_path;
|
||||
@@ -148,15 +118,17 @@ void actor_disrupt(cell_rt *crt)
|
||||
|
||||
JSValue js_os_use(JSContext *js);
|
||||
JSValue js_math_use(JSContext *js);
|
||||
JSValue js_json_use(JSContext *js);
|
||||
JSValue js_nota_use(JSContext *js);
|
||||
JSValue js_wota_use(JSContext *js);
|
||||
|
||||
void script_startup(cell_rt *prt)
|
||||
{
|
||||
JSRuntime *rt = JS_NewRuntime();
|
||||
JSContext *js = JS_NewContextRaw(rt);
|
||||
JS_SetInterruptHandler(rt, (JSInterruptHandler *)actor_interrupt_cb, prt);
|
||||
JSContext *js = JS_NewContext(rt);
|
||||
|
||||
JS_AddIntrinsicBaseObjects(js);
|
||||
JS_AddIntrinsicEval(js);
|
||||
JS_AddIntrinsicRegExp(js);
|
||||
JS_AddIntrinsicJSON(js);
|
||||
|
||||
JS_SetContextOpaque(js, prt);
|
||||
prt->context = js;
|
||||
@@ -182,9 +154,6 @@ void script_startup(cell_rt *prt)
|
||||
// Create hidden environment
|
||||
JSValue hidden_env = JS_NewObject(js);
|
||||
JS_SetPropertyStr(js, hidden_env, "os", js_os_use(js));
|
||||
JS_SetPropertyStr(js, hidden_env, "json", js_json_use(js));
|
||||
JS_SetPropertyStr(js, hidden_env, "nota", js_nota_use(js));
|
||||
JS_SetPropertyStr(js, hidden_env, "wota", js_wota_use(js));
|
||||
|
||||
crt->actor_sym = JS_NewObject(js);
|
||||
JS_SetPropertyStr(js, hidden_env, "actorsym", JS_DupValue(js, crt->actor_sym));
|
||||
@@ -240,13 +209,15 @@ static int run_test_suite(size_t heap_size)
|
||||
return 1;
|
||||
}
|
||||
|
||||
JSContext *ctx = JS_NewContextWithHeapSize(rt, heap_size);
|
||||
JSContext *ctx = JS_NewContextRawWithHeapSize(rt, heap_size);
|
||||
if (!ctx) {
|
||||
printf("Failed to create JS context\n");
|
||||
JS_FreeRuntime(rt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
JS_AddIntrinsicBaseObjects(ctx);
|
||||
|
||||
int result = run_c_test_suite(ctx);
|
||||
|
||||
JS_FreeContext(ctx);
|
||||
@@ -256,7 +227,7 @@ static int run_test_suite(size_t heap_size)
|
||||
}
|
||||
|
||||
/* Run an immediate script string */
|
||||
static int run_eval(const char *script_or_file, int print_bytecode, int use_bootstrap_env)
|
||||
static int run_eval(const char *script_or_file, int print_bytecode)
|
||||
{
|
||||
if (!find_cell_shop()) return 1;
|
||||
|
||||
@@ -296,7 +267,7 @@ static int run_eval(const char *script_or_file, int print_bytecode, int use_boot
|
||||
return 1;
|
||||
}
|
||||
|
||||
JSContext *ctx = JS_NewContext(rt);
|
||||
JSContext *ctx = JS_NewContextRaw(rt);
|
||||
if (!ctx) {
|
||||
printf("Failed to create JS context\n");
|
||||
JS_FreeRuntime(rt);
|
||||
@@ -304,43 +275,23 @@ static int run_eval(const char *script_or_file, int print_bytecode, int use_boot
|
||||
return 1;
|
||||
}
|
||||
|
||||
JS_AddIntrinsicBaseObjects(ctx);
|
||||
JS_AddIntrinsicEval(ctx);
|
||||
JS_AddIntrinsicRegExp(ctx);
|
||||
JS_AddIntrinsicJSON(ctx);
|
||||
|
||||
int result = 0;
|
||||
|
||||
JSGCRef bytecode_ref;
|
||||
JS_PushGCRef(ctx, &bytecode_ref);
|
||||
bytecode_ref.val = JS_Compile(ctx, script, strlen(script), filename);
|
||||
if (JS_IsException(bytecode_ref.val)) {
|
||||
uncaught_exception(ctx, bytecode_ref.val);
|
||||
JS_PopGCRef(ctx, &bytecode_ref);
|
||||
JSValue bytecode = JS_Compile(ctx, script, strlen(script), filename);
|
||||
if (JS_IsException(bytecode)) {
|
||||
uncaught_exception(ctx, bytecode);
|
||||
result = 1;
|
||||
} else {
|
||||
if (print_bytecode) {
|
||||
printf("=== Compiled Bytecode ===\n");
|
||||
JS_DumpFunctionBytecode(ctx, bytecode_ref.val);
|
||||
JS_DumpFunctionBytecode(ctx, bytecode);
|
||||
}
|
||||
JSValue env = JS_NULL;
|
||||
if (use_bootstrap_env) {
|
||||
JSGCRef env_ref, json_ref, nota_ref, wota_ref;
|
||||
JS_PushGCRef(ctx, &env_ref);
|
||||
JS_PushGCRef(ctx, &json_ref);
|
||||
JS_PushGCRef(ctx, ¬a_ref);
|
||||
JS_PushGCRef(ctx, &wota_ref);
|
||||
env_ref.val = JS_NewObject(ctx);
|
||||
/* Create modules with GC rooting, then stone them */
|
||||
json_ref.val = js_json_use(ctx);
|
||||
nota_ref.val = js_nota_use(ctx);
|
||||
wota_ref.val = js_wota_use(ctx);
|
||||
JS_SetPropertyStr(ctx, env_ref.val, "json", JS_Stone(ctx, json_ref.val));
|
||||
JS_SetPropertyStr(ctx, env_ref.val, "nota", JS_Stone(ctx, nota_ref.val));
|
||||
JS_SetPropertyStr(ctx, env_ref.val, "wota", JS_Stone(ctx, wota_ref.val));
|
||||
env = JS_Stone(ctx, env_ref.val);
|
||||
JS_PopGCRef(ctx, &wota_ref);
|
||||
JS_PopGCRef(ctx, ¬a_ref);
|
||||
JS_PopGCRef(ctx, &json_ref);
|
||||
JS_PopGCRef(ctx, &env_ref);
|
||||
}
|
||||
JSValue v = JS_Integrate(ctx, bytecode_ref.val, env);
|
||||
JS_PopGCRef(ctx, &bytecode_ref);
|
||||
JSValue v = JS_Integrate(ctx, bytecode, JS_NULL);
|
||||
if (JS_IsException(v)) {
|
||||
uncaught_exception(ctx, v);
|
||||
result = 1;
|
||||
@@ -370,378 +321,13 @@ int cell_init(int argc, char **argv)
|
||||
return run_test_suite(heap_size);
|
||||
}
|
||||
|
||||
/* Check for --ast flag to output AST JSON */
|
||||
if (argc >= 3 && strcmp(argv[1], "--ast") == 0) {
|
||||
const char *script_or_file = argv[2];
|
||||
char *script = NULL;
|
||||
char *allocated_script = NULL;
|
||||
const char *filename = "<eval>";
|
||||
|
||||
struct stat st;
|
||||
if (stat(script_or_file, &st) == 0 && S_ISREG(st.st_mode)) {
|
||||
FILE *f = fopen(script_or_file, "r");
|
||||
if (!f) {
|
||||
printf("Failed to open file: %s\n", script_or_file);
|
||||
return 1;
|
||||
}
|
||||
allocated_script = malloc(st.st_size + 1);
|
||||
if (!allocated_script) {
|
||||
fclose(f);
|
||||
printf("Failed to allocate memory for script\n");
|
||||
return 1;
|
||||
}
|
||||
size_t read_size = fread(allocated_script, 1, st.st_size, f);
|
||||
fclose(f);
|
||||
allocated_script[read_size] = '\0';
|
||||
script = allocated_script;
|
||||
filename = script_or_file;
|
||||
} else {
|
||||
script = (char *)script_or_file;
|
||||
}
|
||||
|
||||
char *json = JS_AST(script, strlen(script), filename);
|
||||
if (json) {
|
||||
int has_errors = print_json_errors(json);
|
||||
printf("%s\n", json);
|
||||
free(json);
|
||||
free(allocated_script);
|
||||
return has_errors ? 1 : 0;
|
||||
} else {
|
||||
printf("Failed to parse AST\n");
|
||||
free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check for --tokenize flag to output token array JSON */
|
||||
if (argc >= 3 && strcmp(argv[1], "--tokenize") == 0) {
|
||||
const char *script_or_file = argv[2];
|
||||
char *script = NULL;
|
||||
char *allocated_script = NULL;
|
||||
const char *filename = "<eval>";
|
||||
|
||||
struct stat st;
|
||||
if (stat(script_or_file, &st) == 0 && S_ISREG(st.st_mode)) {
|
||||
FILE *f = fopen(script_or_file, "r");
|
||||
if (!f) {
|
||||
printf("Failed to open file: %s\n", script_or_file);
|
||||
return 1;
|
||||
}
|
||||
allocated_script = malloc(st.st_size + 1);
|
||||
if (!allocated_script) {
|
||||
fclose(f);
|
||||
printf("Failed to allocate memory for script\n");
|
||||
return 1;
|
||||
}
|
||||
size_t read_size = fread(allocated_script, 1, st.st_size, f);
|
||||
fclose(f);
|
||||
allocated_script[read_size] = '\0';
|
||||
script = allocated_script;
|
||||
filename = script_or_file;
|
||||
} else {
|
||||
script = (char *)script_or_file;
|
||||
}
|
||||
|
||||
char *json = JS_Tokenize(script, strlen(script), filename);
|
||||
if (json) {
|
||||
int has_errors = print_json_errors(json);
|
||||
printf("%s\n", json);
|
||||
free(json);
|
||||
free(allocated_script);
|
||||
return has_errors ? 1 : 0;
|
||||
} else {
|
||||
printf("Failed to tokenize\n");
|
||||
free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check for --mcode flag to output MCODE JSON IR */
|
||||
if (argc >= 3 && strcmp(argv[1], "--mcode") == 0) {
|
||||
const char *script_or_file = argv[2];
|
||||
char *script = NULL;
|
||||
char *allocated_script = NULL;
|
||||
const char *filename = "<eval>";
|
||||
|
||||
struct stat st;
|
||||
if (stat(script_or_file, &st) == 0 && S_ISREG(st.st_mode)) {
|
||||
FILE *f = fopen(script_or_file, "r");
|
||||
if (!f) {
|
||||
printf("Failed to open file: %s\n", script_or_file);
|
||||
return 1;
|
||||
}
|
||||
allocated_script = malloc(st.st_size + 1);
|
||||
if (!allocated_script) {
|
||||
fclose(f);
|
||||
printf("Failed to allocate memory for script\n");
|
||||
return 1;
|
||||
}
|
||||
size_t read_size = fread(allocated_script, 1, st.st_size, f);
|
||||
fclose(f);
|
||||
allocated_script[read_size] = '\0';
|
||||
script = allocated_script;
|
||||
filename = script_or_file;
|
||||
} else {
|
||||
script = (char *)script_or_file;
|
||||
}
|
||||
|
||||
char *ast_json = JS_AST(script, strlen(script), filename);
|
||||
if (!ast_json) {
|
||||
printf("Failed to parse AST\n");
|
||||
free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (print_json_errors(ast_json)) {
|
||||
free(ast_json);
|
||||
free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *mcode_json = JS_Mcode(ast_json);
|
||||
free(ast_json);
|
||||
|
||||
if (!mcode_json) {
|
||||
printf("Failed to generate MCODE\n");
|
||||
free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("%s\n", mcode_json);
|
||||
free(mcode_json);
|
||||
|
||||
free(allocated_script);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Check for --run-mcode flag to execute via MCODE interpreter */
|
||||
if (argc >= 3 && strcmp(argv[1], "--run-mcode") == 0) {
|
||||
const char *script_or_file = argv[2];
|
||||
char *script = NULL;
|
||||
char *allocated_script = NULL;
|
||||
const char *filename = "<eval>";
|
||||
|
||||
struct stat st;
|
||||
if (stat(script_or_file, &st) == 0 && S_ISREG(st.st_mode)) {
|
||||
FILE *f = fopen(script_or_file, "r");
|
||||
if (!f) { printf("Failed to open file: %s\n", script_or_file); return 1; }
|
||||
allocated_script = malloc(st.st_size + 1);
|
||||
if (!allocated_script) { fclose(f); printf("Failed to allocate memory\n"); return 1; }
|
||||
size_t read_size = fread(allocated_script, 1, st.st_size, f);
|
||||
fclose(f);
|
||||
allocated_script[read_size] = '\0';
|
||||
script = allocated_script;
|
||||
filename = script_or_file;
|
||||
} else {
|
||||
script = (char *)script_or_file;
|
||||
}
|
||||
|
||||
char *ast_json = JS_AST(script, strlen(script), filename);
|
||||
if (!ast_json) {
|
||||
printf("Failed to parse AST\n");
|
||||
free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (print_json_errors(ast_json)) {
|
||||
free(ast_json); free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *mcode_json = JS_Mcode(ast_json);
|
||||
free(ast_json);
|
||||
|
||||
if (!mcode_json) {
|
||||
printf("Failed to generate MCODE\n");
|
||||
free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (print_json_errors(mcode_json)) {
|
||||
free(mcode_json); free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Use a larger heap context for execution */
|
||||
JSRuntime *rt = JS_NewRuntime();
|
||||
if (!rt) { printf("Failed to create JS runtime\n"); free(mcode_json); free(allocated_script); return 1; }
|
||||
JSContext *ctx = JS_NewContextWithHeapSize(rt, 64 * 1024);
|
||||
if (!ctx) { printf("Failed to create execution context\n"); free(mcode_json); JS_FreeRuntime(rt); free(allocated_script); return 1; }
|
||||
|
||||
JSValue result = JS_CallMcode(ctx, mcode_json);
|
||||
free(mcode_json);
|
||||
|
||||
if (JS_IsException(result)) {
|
||||
JSValue exc = JS_GetException(ctx);
|
||||
const char *str = JS_ToCString(ctx, exc);
|
||||
if (str) { printf("Exception: %s\n", str); JS_FreeCString(ctx, str); }
|
||||
} else if (!JS_IsNull(result)) {
|
||||
const char *str = JS_ToCString(ctx, result);
|
||||
if (str) { printf("%s\n", str); JS_FreeCString(ctx, str); }
|
||||
}
|
||||
|
||||
JS_FreeContext(ctx);
|
||||
JS_FreeRuntime(rt);
|
||||
free(allocated_script);
|
||||
return JS_IsException(result) ? 1 : 0;
|
||||
}
|
||||
|
||||
/* Check for --mach flag to dump MACH bytecode */
|
||||
if (argc >= 3 && strcmp(argv[1], "--mach") == 0) {
|
||||
const char *script_or_file = argv[2];
|
||||
char *script = NULL;
|
||||
char *allocated_script = NULL;
|
||||
const char *filename = "<eval>";
|
||||
|
||||
struct stat st;
|
||||
if (stat(script_or_file, &st) == 0 && S_ISREG(st.st_mode)) {
|
||||
FILE *f = fopen(script_or_file, "r");
|
||||
if (!f) {
|
||||
printf("Failed to open file: %s\n", script_or_file);
|
||||
return 1;
|
||||
}
|
||||
allocated_script = malloc(st.st_size + 1);
|
||||
if (!allocated_script) {
|
||||
fclose(f);
|
||||
printf("Failed to allocate memory for script\n");
|
||||
return 1;
|
||||
}
|
||||
size_t read_size = fread(allocated_script, 1, st.st_size, f);
|
||||
fclose(f);
|
||||
allocated_script[read_size] = '\0';
|
||||
script = allocated_script;
|
||||
filename = script_or_file;
|
||||
} else {
|
||||
script = (char *)script_or_file;
|
||||
}
|
||||
|
||||
char *ast_json = JS_AST(script, strlen(script), filename);
|
||||
if (!ast_json) {
|
||||
printf("Failed to parse AST\n");
|
||||
free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (print_json_errors(ast_json)) {
|
||||
free(ast_json);
|
||||
free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
|
||||
JSRuntime *rt = JS_NewRuntime();
|
||||
if (!rt) {
|
||||
printf("Failed to create JS runtime\n");
|
||||
free(ast_json); free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
JSContext *ctx = JS_NewContext(rt);
|
||||
if (!ctx) {
|
||||
printf("Failed to create JS context\n");
|
||||
free(ast_json); JS_FreeRuntime(rt); free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
|
||||
JS_DumpMach(ctx, ast_json, JS_NULL);
|
||||
free(ast_json);
|
||||
|
||||
JS_FreeContext(ctx);
|
||||
JS_FreeRuntime(rt);
|
||||
free(allocated_script);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Check for --mach-run flag to compile and run through MACH VM */
|
||||
if (argc >= 3 && strcmp(argv[1], "--mach-run") == 0) {
|
||||
const char *script_or_file = argv[2];
|
||||
char *script = NULL;
|
||||
char *allocated_script = NULL;
|
||||
const char *filename = "<eval>";
|
||||
|
||||
struct stat st;
|
||||
if (stat(script_or_file, &st) == 0 && S_ISREG(st.st_mode)) {
|
||||
FILE *f = fopen(script_or_file, "r");
|
||||
if (!f) {
|
||||
printf("Failed to open file: %s\n", script_or_file);
|
||||
return 1;
|
||||
}
|
||||
allocated_script = malloc(st.st_size + 1);
|
||||
if (!allocated_script) {
|
||||
fclose(f);
|
||||
printf("Failed to allocate memory for script\n");
|
||||
return 1;
|
||||
}
|
||||
size_t read_size = fread(allocated_script, 1, st.st_size, f);
|
||||
fclose(f);
|
||||
allocated_script[read_size] = '\0';
|
||||
script = allocated_script;
|
||||
filename = script_or_file;
|
||||
} else {
|
||||
script = (char *)script_or_file;
|
||||
}
|
||||
|
||||
char *ast_json = JS_AST(script, strlen(script), filename);
|
||||
if (!ast_json) {
|
||||
printf("Failed to parse AST\n");
|
||||
free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (print_json_errors(ast_json)) {
|
||||
free(ast_json);
|
||||
free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
|
||||
JSRuntime *rt = JS_NewRuntime();
|
||||
if (!rt) {
|
||||
printf("Failed to create JS runtime\n");
|
||||
free(ast_json); free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
JSContext *ctx = JS_NewContext(rt);
|
||||
if (!ctx) {
|
||||
printf("Failed to create JS context\n");
|
||||
free(ast_json); JS_FreeRuntime(rt); free(allocated_script);
|
||||
return 1;
|
||||
}
|
||||
|
||||
JSValue result = JS_RunMach(ctx, ast_json, JS_NULL);
|
||||
free(ast_json);
|
||||
|
||||
int exit_code = 0;
|
||||
if (JS_IsException(result)) {
|
||||
JSValue exc = JS_GetException(ctx);
|
||||
const char *err_str = JS_ToCString(ctx, exc);
|
||||
if (err_str) {
|
||||
printf("Error: %s\n", err_str);
|
||||
JS_FreeCString(ctx, err_str);
|
||||
}
|
||||
exit_code = 1;
|
||||
} else if (!JS_IsNull(result)) {
|
||||
const char *str = JS_ToCString(ctx, result);
|
||||
if (str) {
|
||||
printf("%s\n", str);
|
||||
JS_FreeCString(ctx, str);
|
||||
}
|
||||
}
|
||||
|
||||
JS_FreeContext(ctx);
|
||||
JS_FreeRuntime(rt);
|
||||
free(allocated_script);
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
/* Check for -e or --eval flag to run immediate script */
|
||||
/* Also check for -p flag to print bytecode */
|
||||
/* -s / --serializers flag provides json, nota, wota in env */
|
||||
if (argc >= 3 && (strcmp(argv[1], "-e") == 0 || strcmp(argv[1], "--eval") == 0)) {
|
||||
return run_eval(argv[2], 0, 0);
|
||||
return run_eval(argv[2], 0);
|
||||
}
|
||||
if (argc >= 3 && (strcmp(argv[1], "-p") == 0 || strcmp(argv[1], "--print-bytecode") == 0)) {
|
||||
return run_eval(argv[2], 1, 0);
|
||||
}
|
||||
if (argc >= 3 && (strcmp(argv[1], "-s") == 0 || strcmp(argv[1], "--serializers") == 0)) {
|
||||
return run_eval(argv[2], 0, 1);
|
||||
return run_eval(argv[2], 1);
|
||||
}
|
||||
|
||||
int script_start = 1;
|
||||
|
||||
@@ -28,13 +28,6 @@ JSValue number2js(JSContext *js, double g);
|
||||
JSValue wota2value(JSContext *js, void *v);
|
||||
void *value2wota(JSContext *js, JSValue v, JSValue replacer, size_t *bytes);
|
||||
|
||||
JSValue nota2value(JSContext *js, void *nota);
|
||||
void *value2nota(JSContext *js, JSValue v);
|
||||
|
||||
JSValue js_json_use(JSContext *js);
|
||||
JSValue js_nota_use(JSContext *js);
|
||||
JSValue js_wota_use(JSContext *js);
|
||||
|
||||
#define CELL_HOOK_ENTER 1
|
||||
#define CELL_HOOK_EXIT 2
|
||||
typedef void (*cell_hook)(const char *name, int type);
|
||||
|
||||
401
source/qjs_wota.c
Normal file
401
source/qjs_wota.c
Normal file
@@ -0,0 +1,401 @@
|
||||
#include "cell.h"
|
||||
#include "cell_internal.h"
|
||||
|
||||
#include "wota.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct ObjectRef {
|
||||
void *ptr;
|
||||
struct ObjectRef *next;
|
||||
} ObjectRef;
|
||||
|
||||
typedef struct WotaEncodeContext {
|
||||
JSContext *ctx;
|
||||
ObjectRef *visited_stack;
|
||||
WotaBuffer wb;
|
||||
int cycle;
|
||||
JSValue replacer;
|
||||
} WotaEncodeContext;
|
||||
|
||||
static void wota_stack_push(WotaEncodeContext *enc, JSValueConst val)
|
||||
{
|
||||
/* if (!JS_IsObject(val)) return;
|
||||
|
||||
ObjectRef *ref = malloc(sizeof(ObjectRef));
|
||||
if (!ref) return;
|
||||
|
||||
ref->ptr = JS_VALUE_GET_PTR(val);
|
||||
ref->next = enc->visited_stack;
|
||||
enc->visited_stack = ref;*/
|
||||
}
|
||||
|
||||
static void wota_stack_pop(WotaEncodeContext *enc)
|
||||
{
|
||||
if (!enc->visited_stack) return;
|
||||
|
||||
ObjectRef *top = enc->visited_stack;
|
||||
enc->visited_stack = top->next;
|
||||
free(top);
|
||||
}
|
||||
|
||||
static int wota_stack_has(WotaEncodeContext *enc, JSValueConst val)
|
||||
{
|
||||
/* if (!JS_IsObject(val)) return 0;
|
||||
|
||||
void *ptr = JS_VALUE_GET_PTR(val);
|
||||
ObjectRef *current = enc->visited_stack;
|
||||
|
||||
while (current) {
|
||||
if (current->ptr == ptr) return 1;
|
||||
current = current->next;
|
||||
}
|
||||
return 0;*/
|
||||
}
|
||||
|
||||
|
||||
static void wota_stack_free(WotaEncodeContext *enc)
|
||||
{
|
||||
while (enc->visited_stack) {
|
||||
wota_stack_pop(enc);
|
||||
}
|
||||
}
|
||||
|
||||
static JSValue apply_replacer(WotaEncodeContext *enc, JSValueConst holder, JSValue key, JSValueConst val)
|
||||
{
|
||||
if (JS_IsNull(enc->replacer)) return JS_DupValue(enc->ctx, val);
|
||||
JSValue key_val = JS_IsNull(key) ? JS_NULL : JS_DupValue(enc->ctx, key);
|
||||
JSValue args[2] = { key_val, JS_DupValue(enc->ctx, val) };
|
||||
JSValue result = JS_Call(enc->ctx, enc->replacer, holder, 2, args);
|
||||
JS_FreeValue(enc->ctx, args[0]);
|
||||
JS_FreeValue(enc->ctx, args[1]);
|
||||
if (JS_IsException(result)) return JS_DupValue(enc->ctx, val);
|
||||
return result;
|
||||
}
|
||||
|
||||
static void wota_encode_value(WotaEncodeContext *enc, JSValueConst val, JSValueConst holder, JSValue key);
|
||||
|
||||
static void encode_object_properties(WotaEncodeContext *enc, JSValueConst val, JSValueConst holder)
|
||||
{
|
||||
JSContext *ctx = enc->ctx;
|
||||
JSValue keys = JS_GetOwnPropertyNames(ctx, val);
|
||||
if (JS_IsException(keys)) {
|
||||
wota_write_sym(&enc->wb, WOTA_NULL);
|
||||
return;
|
||||
}
|
||||
int64_t plen64;
|
||||
if (JS_GetLength(ctx, keys, &plen64) < 0) {
|
||||
JS_FreeValue(ctx, keys);
|
||||
wota_write_sym(&enc->wb, WOTA_NULL);
|
||||
return;
|
||||
}
|
||||
uint32_t plen = (uint32_t)plen64;
|
||||
uint32_t non_function_count = 0;
|
||||
JSValue props[plen];
|
||||
JSValue kept_keys[plen];
|
||||
|
||||
for (uint32_t i = 0; i < plen; i++) {
|
||||
JSValue key = JS_GetPropertyUint32(ctx, keys, i);
|
||||
JSValue prop_val = JS_GetProperty(ctx, val, key);
|
||||
if (!JS_IsFunction(prop_val)) {
|
||||
kept_keys[non_function_count] = key;
|
||||
props[non_function_count++] = prop_val;
|
||||
} else {
|
||||
JS_FreeValue(ctx, prop_val);
|
||||
JS_FreeValue(ctx, key);
|
||||
}
|
||||
}
|
||||
JS_FreeValue(ctx, keys);
|
||||
wota_write_record(&enc->wb, non_function_count);
|
||||
for (uint32_t i = 0; i < non_function_count; i++) {
|
||||
size_t klen;
|
||||
const char *prop_name = JS_ToCStringLen(ctx, &klen, kept_keys[i]);
|
||||
JSValue prop_val = props[i];
|
||||
wota_write_text_len(&enc->wb, prop_name ? prop_name : "", prop_name ? klen : 0);
|
||||
wota_encode_value(enc, prop_val, val, kept_keys[i]);
|
||||
JS_FreeCString(ctx, prop_name);
|
||||
JS_FreeValue(ctx, prop_val);
|
||||
JS_FreeValue(ctx, kept_keys[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void wota_encode_value(WotaEncodeContext *enc, JSValueConst val, JSValueConst holder, JSValue key)
|
||||
{
|
||||
JSContext *ctx = enc->ctx;
|
||||
JSValue replaced;
|
||||
if (!JS_IsNull(enc->replacer) && !JS_IsNull(key))
|
||||
replaced = apply_replacer(enc, holder, key, val);
|
||||
else
|
||||
replaced = JS_DupValue(enc->ctx, val);
|
||||
|
||||
int tag = JS_VALUE_GET_TAG(replaced);
|
||||
switch (tag) {
|
||||
case JS_TAG_INT: {
|
||||
int32_t d;
|
||||
JS_ToInt32(ctx, &d, replaced);
|
||||
wota_write_int_word(&enc->wb, d);
|
||||
break;
|
||||
}
|
||||
case JS_TAG_FLOAT64: {
|
||||
double d;
|
||||
if (JS_ToFloat64(ctx, &d, replaced) < 0) {
|
||||
wota_write_sym(&enc->wb, WOTA_NULL);
|
||||
break;
|
||||
}
|
||||
wota_write_float_word(&enc->wb, d);
|
||||
break;
|
||||
}
|
||||
case JS_TAG_STRING: {
|
||||
size_t plen;
|
||||
const char *str = JS_ToCStringLen(ctx, &plen, replaced);
|
||||
wota_write_text_len(&enc->wb, str ? str : "", str ? plen : 0);
|
||||
JS_FreeCString(ctx, str);
|
||||
break;
|
||||
}
|
||||
case JS_TAG_BOOL:
|
||||
wota_write_sym(&enc->wb, JS_VALUE_GET_BOOL(replaced) ? WOTA_TRUE : WOTA_FALSE);
|
||||
break;
|
||||
case JS_TAG_NULL:
|
||||
wota_write_sym(&enc->wb, WOTA_NULL);
|
||||
break;
|
||||
case JS_TAG_PTR: {
|
||||
if (js_is_blob(ctx, replaced)) {
|
||||
size_t buf_len;
|
||||
void *buf_data = js_get_blob_data(ctx, &buf_len, replaced);
|
||||
if (buf_data == (void *)-1) {
|
||||
JS_FreeValue(ctx, replaced);
|
||||
return; // JS_EXCEPTION will be handled by caller
|
||||
}
|
||||
if (buf_len == 0) {
|
||||
wota_write_blob(&enc->wb, 0, "");
|
||||
} else {
|
||||
wota_write_blob(&enc->wb, (unsigned long long)buf_len * 8, (const char *)buf_data);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (JS_IsArray(replaced)) {
|
||||
if (wota_stack_has(enc, replaced)) {
|
||||
enc->cycle = 1;
|
||||
break;
|
||||
}
|
||||
wota_stack_push(enc, replaced);
|
||||
int64_t arr_len;
|
||||
JS_GetLength(ctx, replaced, &arr_len);
|
||||
wota_write_array(&enc->wb, arr_len);
|
||||
for (int64_t i = 0; i < arr_len; i++) {
|
||||
JSValue elem_val = JS_GetPropertyUint32(ctx, replaced, i);
|
||||
/* Use int index as key placeholder */
|
||||
wota_encode_value(enc, elem_val, replaced, JS_NewInt32(ctx, (int32_t)i));
|
||||
JS_FreeValue(ctx, elem_val);
|
||||
}
|
||||
wota_stack_pop(enc);
|
||||
break;
|
||||
}
|
||||
cell_rt *crt = JS_GetContextOpaque(ctx);
|
||||
// JSValue adata = JS_GetProperty(ctx, replaced, crt->actor_sym);
|
||||
JSValue adata = JS_NULL;
|
||||
if (!JS_IsNull(adata)) {
|
||||
wota_write_sym(&enc->wb, WOTA_PRIVATE);
|
||||
wota_encode_value(enc, adata, replaced, JS_NULL);
|
||||
JS_FreeValue(ctx, adata);
|
||||
break;
|
||||
}
|
||||
JS_FreeValue(ctx, adata);
|
||||
if (wota_stack_has(enc, replaced)) {
|
||||
enc->cycle = 1;
|
||||
break;
|
||||
}
|
||||
wota_stack_push(enc, replaced);
|
||||
JSValue to_json = JS_GetPropertyStr(ctx, replaced, "toJSON");
|
||||
if (JS_IsFunction(to_json)) {
|
||||
JSValue result = JS_Call(ctx, to_json, replaced, 0, NULL);
|
||||
JS_FreeValue(ctx, to_json);
|
||||
if (!JS_IsException(result)) {
|
||||
wota_encode_value(enc, result, holder, key);
|
||||
JS_FreeValue(ctx, result);
|
||||
} else
|
||||
wota_write_sym(&enc->wb, WOTA_NULL);
|
||||
wota_stack_pop(enc);
|
||||
break;
|
||||
}
|
||||
JS_FreeValue(ctx, to_json);
|
||||
encode_object_properties(enc, replaced, holder);
|
||||
wota_stack_pop(enc);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
wota_write_sym(&enc->wb, WOTA_NULL);
|
||||
break;
|
||||
}
|
||||
JS_FreeValue(ctx, replaced);
|
||||
}
|
||||
|
||||
static char *decode_wota_value(JSContext *ctx, char *data_ptr, JSValue *out_val, JSValue holder, JSValue key, JSValue reviver)
|
||||
{
|
||||
uint64_t first_word = *(uint64_t *)data_ptr;
|
||||
int type = (int)(first_word & 0xffU);
|
||||
switch (type) {
|
||||
case WOTA_INT: {
|
||||
long long val;
|
||||
data_ptr = wota_read_int(&val, data_ptr);
|
||||
*out_val = JS_NewInt64(ctx, val);
|
||||
break;
|
||||
}
|
||||
case WOTA_FLOAT: {
|
||||
double d;
|
||||
data_ptr = wota_read_float(&d, data_ptr);
|
||||
*out_val = JS_NewFloat64(ctx, d);
|
||||
break;
|
||||
}
|
||||
case WOTA_SYM: {
|
||||
int scode;
|
||||
data_ptr = wota_read_sym(&scode, data_ptr);
|
||||
if (scode == WOTA_PRIVATE) {
|
||||
JSValue inner = JS_NULL;
|
||||
data_ptr = decode_wota_value(ctx, data_ptr, &inner, holder, JS_NULL, reviver);
|
||||
JSValue obj = JS_NewObject(ctx);
|
||||
cell_rt *crt = JS_GetContextOpaque(ctx);
|
||||
// JS_SetProperty(ctx, obj, crt->actor_sym, inner);
|
||||
*out_val = obj;
|
||||
} else if (scode == WOTA_NULL) *out_val = JS_NULL;
|
||||
else if (scode == WOTA_FALSE) *out_val = JS_NewBool(ctx, 0);
|
||||
else if (scode == WOTA_TRUE) *out_val = JS_NewBool(ctx, 1);
|
||||
else *out_val = JS_NULL;
|
||||
break;
|
||||
}
|
||||
case WOTA_BLOB: {
|
||||
long long blen;
|
||||
char *bdata = NULL;
|
||||
data_ptr = wota_read_blob(&blen, &bdata, data_ptr);
|
||||
*out_val = bdata ? js_new_blob_stoned_copy(ctx, (uint8_t *)bdata, (size_t)blen) : js_new_blob_stoned_copy(ctx, NULL, 0);
|
||||
if (bdata) free(bdata);
|
||||
break;
|
||||
}
|
||||
case WOTA_TEXT: {
|
||||
char *utf8 = NULL;
|
||||
data_ptr = wota_read_text(&utf8, data_ptr);
|
||||
*out_val = JS_NewString(ctx, utf8 ? utf8 : "");
|
||||
if (utf8) free(utf8);
|
||||
break;
|
||||
}
|
||||
case WOTA_ARR: {
|
||||
long long c;
|
||||
data_ptr = wota_read_array(&c, data_ptr);
|
||||
JSValue arr = JS_NewArrayLen(ctx, c);
|
||||
for (long long i = 0; i < c; i++) {
|
||||
JSValue elem_val = JS_NULL;
|
||||
JSValue idx_key = JS_NewInt32(ctx, (int32_t)i);
|
||||
data_ptr = decode_wota_value(ctx, data_ptr, &elem_val, arr, idx_key, reviver);
|
||||
JS_SetPropertyUint32(ctx, arr, i, elem_val);
|
||||
}
|
||||
*out_val = arr;
|
||||
break;
|
||||
}
|
||||
case WOTA_REC: {
|
||||
long long c;
|
||||
data_ptr = wota_read_record(&c, data_ptr);
|
||||
JSValue obj = JS_NewObject(ctx);
|
||||
for (long long i = 0; i < c; i++) {
|
||||
char *tkey = NULL;
|
||||
size_t key_len;
|
||||
data_ptr = wota_read_text_len(&key_len, &tkey, data_ptr);
|
||||
if (!tkey) continue; // invalid key
|
||||
JSValue prop_key = JS_NewStringLen(ctx, tkey, key_len);
|
||||
JSValue sub_val = JS_NULL;
|
||||
data_ptr = decode_wota_value(ctx, data_ptr, &sub_val, obj, prop_key, reviver);
|
||||
JS_SetProperty(ctx, obj, prop_key, sub_val);
|
||||
JS_FreeValue(ctx, prop_key);
|
||||
free(tkey);
|
||||
}
|
||||
*out_val = obj;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
data_ptr += 8;
|
||||
*out_val = JS_NULL;
|
||||
break;
|
||||
}
|
||||
if (!JS_IsNull(reviver)) {
|
||||
JSValue key_val = JS_IsNull(key) ? JS_NULL : JS_DupValue(ctx, key);
|
||||
JSValue args[2] = { key_val, JS_DupValue(ctx, *out_val) };
|
||||
JSValue revived = JS_Call(ctx, reviver, holder, 2, args);
|
||||
JS_FreeValue(ctx, args[0]);
|
||||
JS_FreeValue(ctx, args[1]);
|
||||
if (!JS_IsException(revived)) {
|
||||
JS_FreeValue(ctx, *out_val);
|
||||
*out_val = revived;
|
||||
} else
|
||||
JS_FreeValue(ctx, revived);
|
||||
}
|
||||
return data_ptr;
|
||||
}
|
||||
|
||||
void *value2wota(JSContext *ctx, JSValue v, JSValue replacer, size_t *bytes)
|
||||
{
|
||||
WotaEncodeContext enc_s, *enc = &enc_s;
|
||||
|
||||
enc->ctx = ctx;
|
||||
enc->visited_stack = NULL;
|
||||
enc->cycle = 0;
|
||||
enc->replacer = replacer;
|
||||
wota_buffer_init(&enc->wb, 16);
|
||||
wota_encode_value(enc, v, JS_NULL, JS_NULL);
|
||||
if (enc->cycle) {
|
||||
wota_stack_free(enc);
|
||||
wota_buffer_free(&enc->wb);
|
||||
return NULL;
|
||||
}
|
||||
wota_stack_free(enc);
|
||||
size_t total_bytes = enc->wb.size * sizeof(uint64_t);
|
||||
void *wota = realloc(enc->wb.data, total_bytes);
|
||||
if (bytes) *bytes = total_bytes;
|
||||
return wota;
|
||||
}
|
||||
|
||||
JSValue wota2value(JSContext *ctx, void *wota)
|
||||
{
|
||||
JSValue result = JS_NULL;
|
||||
JSValue holder = JS_NewObject(ctx);
|
||||
decode_wota_value(ctx, wota, &result, holder, JS_NULL, JS_NULL);
|
||||
JS_FreeValue(ctx, holder);
|
||||
return result;
|
||||
}
|
||||
|
||||
static JSValue js_wota_encode(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
|
||||
{
|
||||
if (argc < 1) return JS_ThrowTypeError(ctx, "wota.encode requires at least 1 argument");
|
||||
size_t total_bytes;
|
||||
void *wota = value2wota(ctx, argv[0], JS_IsFunction(argv[1]) ? argv[1] : JS_NULL, &total_bytes);
|
||||
JSValue ret = js_new_blob_stoned_copy(ctx, wota, total_bytes);
|
||||
free(wota);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static JSValue js_wota_decode(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
|
||||
{
|
||||
if (argc < 1) return JS_NULL;
|
||||
size_t len;
|
||||
uint8_t *buf = js_get_blob_data(ctx, &len, argv[0]);
|
||||
if (buf == (uint8_t *)-1) return JS_EXCEPTION;
|
||||
if (!buf || len == 0) return JS_ThrowTypeError(ctx, "No blob data present");
|
||||
JSValue reviver = (argc > 1 && JS_IsFunction(argv[1])) ? argv[1] : JS_NULL;
|
||||
char *data_ptr = (char *)buf;
|
||||
JSValue result = JS_NULL;
|
||||
JSValue holder = JS_NewObject(ctx);
|
||||
JSValue empty_key = JS_NewString(ctx, "");
|
||||
decode_wota_value(ctx, data_ptr, &result, holder, empty_key, reviver);
|
||||
JS_FreeValue(ctx, empty_key);
|
||||
JS_FreeValue(ctx, holder);
|
||||
return result;
|
||||
}
|
||||
|
||||
static const JSCFunctionListEntry js_wota_funcs[] = {
|
||||
JS_CFUNC_DEF("encode", 2, js_wota_encode),
|
||||
JS_CFUNC_DEF("decode", 2, js_wota_decode),
|
||||
};
|
||||
|
||||
JSValue js_wota_use(JSContext *ctx)
|
||||
{
|
||||
JSValue exports = JS_NewObject(ctx);
|
||||
JS_SetPropertyFunctionList(ctx, exports, js_wota_funcs, sizeof(js_wota_funcs)/sizeof(js_wota_funcs[0]));
|
||||
return exports;
|
||||
}
|
||||
@@ -205,7 +205,6 @@ DEF( set_up, 4, 1, 0, u8_u16) /* value, depth:u8, slot:u16 -> */
|
||||
/* Name resolution with bytecode patching */
|
||||
DEF( get_name, 5, 0, 1, const) /* cpool_idx -> value, patches itself */
|
||||
DEF( get_env_slot, 3, 0, 1, u16) /* slot -> value (patched from get_name) */
|
||||
DEF( set_env_slot, 3, 1, 0, u16) /* value -> slot (patched from put_var) */
|
||||
DEF(get_global_slot, 3, 0, 1, u16) /* slot -> value (patched from get_var) */
|
||||
DEF(set_global_slot, 3, 1, 0, u16) /* value -> slot (patched from put_var) */
|
||||
|
||||
|
||||
11916
source/quickjs.c
11916
source/quickjs.c
File diff suppressed because it is too large
Load Diff
248
source/quickjs.h
248
source/quickjs.h
@@ -92,7 +92,6 @@ static inline int objhdr_s (objhdr_t h) { return (h & OBJHDR_S_MASK) != 0; }
|
||||
typedef struct JSRuntime JSRuntime; // the entire VM
|
||||
typedef struct JSContext JSContext; // Each actor
|
||||
typedef struct JSClass JSClass;
|
||||
typedef struct JSFunctionBytecode JSFunctionBytecode;
|
||||
typedef uint32_t JSClassID;
|
||||
|
||||
/* Forward declaration - JSGCRef moved after JSValue definition */
|
||||
@@ -177,6 +176,8 @@ void JS_DeleteGCRef(JSContext *ctx, JSGCRef *ref);
|
||||
Value Extraction
|
||||
============================================================ */
|
||||
|
||||
#define JS_VALUE_GET_INT(v) ((int)(v) >> 1)
|
||||
|
||||
/* Get primary tag (low 2-3 bits) */
|
||||
static inline int
|
||||
JS_VALUE_GET_TAG (JSValue v) {
|
||||
@@ -333,8 +334,13 @@ JS_IsShortFloat (JSValue v) {
|
||||
#define JS_DEFAULT_STACK_SIZE (1024 * 1024)
|
||||
#endif
|
||||
|
||||
/* Internal compile flags */
|
||||
/* Internal eval flags */
|
||||
#define JS_EVAL_TYPE_GLOBAL (0 << 0) /* global code (default) */
|
||||
#define JS_EVAL_TYPE_DIRECT (2 << 0) /* direct call (internal use) */
|
||||
#define JS_EVAL_TYPE_INDIRECT (3 << 0) /* indirect call (internal use) */
|
||||
#define JS_EVAL_TYPE_MASK (3 << 0)
|
||||
#define JS_EVAL_FLAG_COMPILE_ONLY (1 << 5) /* internal use */
|
||||
#define JS_EVAL_FLAG_BACKTRACE_BARRIER (1 << 6) /* internal use */
|
||||
|
||||
typedef JSValue JSCFunction (JSContext *ctx, JSValue this_val, int argc,
|
||||
JSValue *argv);
|
||||
@@ -389,7 +395,10 @@ JSRuntime *JS_GetRuntime (JSContext *ctx);
|
||||
void JS_SetClassProto (JSContext *ctx, JSClassID class_id, JSValue obj);
|
||||
JSValue JS_GetClassProto (JSContext *ctx, JSClassID class_id);
|
||||
|
||||
JSContext *JS_NewContextWithHeapSize (JSRuntime *rt, size_t heap_size);
|
||||
/* the following functions are used to select the intrinsic object to
|
||||
save memory */
|
||||
JSContext *JS_NewContextRaw (JSRuntime *rt);
|
||||
JSContext *JS_NewContextRawWithHeapSize (JSRuntime *rt, size_t heap_size);
|
||||
|
||||
typedef struct JSMemoryUsage {
|
||||
int64_t malloc_size, malloc_limit, memory_used_size;
|
||||
@@ -724,6 +733,7 @@ JSValue JS_Compile (JSContext *ctx, const char *input, size_t input_len,
|
||||
env should be stoned record or null.
|
||||
Variables resolve: env first, then global intrinsics. */
|
||||
JSValue JS_Integrate (JSContext *ctx, JSValue bytecode, JSValue env);
|
||||
JSValue JS_GetGlobalObject (JSContext *ctx);
|
||||
void JS_SetOpaque (JSValue obj, void *opaque);
|
||||
void *JS_GetOpaque (JSValue obj, JSClassID class_id);
|
||||
void *JS_GetOpaque2 (JSContext *ctx, JSValue obj, JSClassID class_id);
|
||||
@@ -783,14 +793,7 @@ typedef enum JSCFunctionEnum {
|
||||
JS_CFUNC_1, /* JSValue f(ctx, this_val, arg0) */
|
||||
JS_CFUNC_2, /* JSValue f(ctx, this_val, arg0, arg1) */
|
||||
JS_CFUNC_3, /* JSValue f(ctx, this_val, arg0, arg1, arg2) */
|
||||
JS_CFUNC_4,
|
||||
/* Pure functions (no this_val) - for global utility functions */
|
||||
JS_CFUNC_PURE, /* JSValue f(ctx, argc, argv) - generic pure */
|
||||
JS_CFUNC_PURE_0, /* JSValue f(ctx) */
|
||||
JS_CFUNC_PURE_1, /* JSValue f(ctx, arg0) */
|
||||
JS_CFUNC_PURE_2, /* JSValue f(ctx, arg0, arg1) */
|
||||
JS_CFUNC_PURE_3, /* JSValue f(ctx, arg0, arg1, arg2) */
|
||||
JS_CFUNC_PURE_4 /* JSValue f(ctx, arg0, arg1, arg2, arg3) */
|
||||
JS_CFUNC_4
|
||||
} JSCFunctionEnum;
|
||||
|
||||
/* Fixed-arity C function types for fast paths */
|
||||
@@ -806,16 +809,6 @@ typedef JSValue JSCFunction4 (JSContext *ctx, JSValue this_val,
|
||||
JSValue arg0, JSValue arg1,
|
||||
JSValue arg2, JSValue arg3);
|
||||
|
||||
/* Pure function types (no this_val) */
|
||||
typedef JSValue JSCFunctionPure (JSContext *ctx, int argc, JSValue *argv);
|
||||
typedef JSValue JSCFunctionPure0 (JSContext *ctx);
|
||||
typedef JSValue JSCFunctionPure1 (JSContext *ctx, JSValue arg0);
|
||||
typedef JSValue JSCFunctionPure2 (JSContext *ctx, JSValue arg0, JSValue arg1);
|
||||
typedef JSValue JSCFunctionPure3 (JSContext *ctx, JSValue arg0, JSValue arg1,
|
||||
JSValue arg2);
|
||||
typedef JSValue JSCFunctionPure4 (JSContext *ctx, JSValue arg0, JSValue arg1,
|
||||
JSValue arg2, JSValue arg3);
|
||||
|
||||
typedef union JSCFunctionType {
|
||||
JSCFunction *generic;
|
||||
JSValue (*generic_magic) (JSContext *ctx, JSValue this_val, int argc,
|
||||
@@ -828,13 +821,6 @@ typedef union JSCFunctionType {
|
||||
JSCFunction2 *f2;
|
||||
JSCFunction3 *f3;
|
||||
JSCFunction4 *f4;
|
||||
/* Pure function pointers */
|
||||
JSCFunctionPure *pure;
|
||||
JSCFunctionPure0 *pure0;
|
||||
JSCFunctionPure1 *pure1;
|
||||
JSCFunctionPure2 *pure2;
|
||||
JSCFunctionPure3 *pure3;
|
||||
JSCFunctionPure4 *pure4;
|
||||
} JSCFunctionType;
|
||||
|
||||
JSValue JS_NewCFunction2 (JSContext *ctx, JSCFunction *func, const char *name,
|
||||
@@ -951,43 +937,6 @@ typedef struct JSCFunctionListEntry {
|
||||
.u \
|
||||
= {.func = { 3, JS_CFUNC_3, { .f3 = func1 } } } \
|
||||
}
|
||||
/* Pure function (no this_val) macros */
|
||||
#define JS_CFUNC_PURE_DEF(name, length, func1) \
|
||||
{ \
|
||||
name, 0, JS_DEF_CFUNC, 0, \
|
||||
.u \
|
||||
= {.func = { length, JS_CFUNC_PURE, { .pure = func1 } } } \
|
||||
}
|
||||
#define JS_CFUNC_PURE0_DEF(name, func1) \
|
||||
{ \
|
||||
name, 0, JS_DEF_CFUNC, 0, \
|
||||
.u \
|
||||
= {.func = { 0, JS_CFUNC_PURE_0, { .pure0 = func1 } } } \
|
||||
}
|
||||
#define JS_CFUNC_PURE1_DEF(name, func1) \
|
||||
{ \
|
||||
name, 0, JS_DEF_CFUNC, 0, \
|
||||
.u \
|
||||
= {.func = { 1, JS_CFUNC_PURE_1, { .pure1 = func1 } } } \
|
||||
}
|
||||
#define JS_CFUNC_PURE2_DEF(name, func1) \
|
||||
{ \
|
||||
name, 0, JS_DEF_CFUNC, 0, \
|
||||
.u \
|
||||
= {.func = { 2, JS_CFUNC_PURE_2, { .pure2 = func1 } } } \
|
||||
}
|
||||
#define JS_CFUNC_PURE3_DEF(name, func1) \
|
||||
{ \
|
||||
name, 0, JS_DEF_CFUNC, 0, \
|
||||
.u \
|
||||
= {.func = { 3, JS_CFUNC_PURE_3, { .pure3 = func1 } } } \
|
||||
}
|
||||
#define JS_CFUNC_PURE4_DEF(name, func1) \
|
||||
{ \
|
||||
name, 0, JS_DEF_CFUNC, 0, \
|
||||
.u \
|
||||
= {.func = { 4, JS_CFUNC_PURE_4, { .pure4 = func1 } } } \
|
||||
}
|
||||
#define JS_ITERATOR_NEXT_DEF(name, length, func1, magic) \
|
||||
{ \
|
||||
name, 0, JS_DEF_CFUNC, magic, .u = { \
|
||||
@@ -1100,169 +1049,12 @@ void *js_malloc_rt (size_t size);
|
||||
void *js_mallocz_rt (size_t size);
|
||||
void js_free_rt (void *ptr);
|
||||
|
||||
/* ============================================================================
|
||||
Context-Neutral Module Format (CellModule)
|
||||
============================================================================ */
|
||||
|
||||
/* Capture descriptor - what a nested function closes over */
|
||||
typedef enum {
|
||||
CAP_FROM_PARENT_LOCAL = 1, /* capture local from parent function */
|
||||
CAP_FROM_PARENT_UPVALUE = 2 /* forward upvalue from parent's upvalues */
|
||||
} CellCapKind;
|
||||
|
||||
typedef struct CellCapDesc {
|
||||
uint8_t kind; /* CAP_FROM_PARENT_LOCAL or CAP_FROM_PARENT_UPVALUE */
|
||||
uint16_t index; /* local index in parent, or upvalue index in parent */
|
||||
} CellCapDesc;
|
||||
|
||||
/* External relocation - for integrate-time patching */
|
||||
typedef enum {
|
||||
EXT_GET = 1, /* OP_get_var -> OP_get_env_slot or OP_get_global_slot */
|
||||
EXT_SET = 2 /* OP_put_var -> OP_set_env_slot or OP_set_global_slot */
|
||||
} CellExtKind;
|
||||
|
||||
typedef struct CellExternalReloc {
|
||||
uint32_t pc_offset; /* where operand lives in bytecode */
|
||||
uint32_t name_sid; /* string id of the external name */
|
||||
uint8_t kind; /* EXT_GET or EXT_SET */
|
||||
} CellExternalReloc;
|
||||
|
||||
/* Constant types in cpool */
|
||||
typedef enum {
|
||||
CELL_CONST_NULL = 0,
|
||||
CELL_CONST_INT = 1,
|
||||
CELL_CONST_FLOAT = 2,
|
||||
CELL_CONST_STRING = 3, /* string_sid into module string table */
|
||||
CELL_CONST_UNIT = 4 /* unit_id for nested function */
|
||||
} CellConstType;
|
||||
|
||||
typedef struct CellConst {
|
||||
uint8_t type; /* CellConstType */
|
||||
union {
|
||||
int32_t i32;
|
||||
double f64;
|
||||
uint32_t string_sid;
|
||||
uint32_t unit_id;
|
||||
};
|
||||
} CellConst;
|
||||
|
||||
/* Per-unit structure (context-neutral, flattened) */
|
||||
typedef struct CellUnit {
|
||||
/* Constant pool */
|
||||
uint32_t const_count;
|
||||
CellConst *constants;
|
||||
|
||||
/* Bytecode */
|
||||
uint32_t bytecode_len;
|
||||
uint8_t *bytecode;
|
||||
|
||||
/* Stack requirements */
|
||||
uint16_t arg_count;
|
||||
uint16_t var_count;
|
||||
uint16_t stack_size;
|
||||
|
||||
/* Upvalue (capture) descriptors */
|
||||
uint16_t upvalue_count;
|
||||
CellCapDesc *upvalues;
|
||||
|
||||
/* External relocations */
|
||||
uint32_t external_count;
|
||||
CellExternalReloc *externals;
|
||||
|
||||
/* Debug info (optional) */
|
||||
uint32_t pc2line_len;
|
||||
uint8_t *pc2line;
|
||||
uint32_t name_sid; /* unit name for stack traces */
|
||||
} CellUnit;
|
||||
|
||||
/* Module-level structure (context-neutral) */
|
||||
#define CELL_MODULE_MAGIC 0x4C4C4543 /* "CELL" */
|
||||
#define CELL_MODULE_VERSION 1
|
||||
|
||||
typedef struct CellModule {
|
||||
uint32_t magic; /* CELL_MODULE_MAGIC */
|
||||
uint8_t version; /* CELL_MODULE_VERSION */
|
||||
uint8_t flags;
|
||||
|
||||
/* Shared string table (module-global) */
|
||||
uint32_t string_count;
|
||||
uint32_t string_data_size;
|
||||
uint8_t *string_data; /* concatenated UTF-8 strings */
|
||||
uint32_t *string_offsets; /* offset for each string */
|
||||
|
||||
/* Unit table (entry 0 is the main/entry unit) */
|
||||
uint32_t unit_count;
|
||||
CellUnit *units;
|
||||
|
||||
/* Debug: source stored once at module level */
|
||||
uint32_t source_len;
|
||||
char *source;
|
||||
} CellModule;
|
||||
|
||||
/* Free a CellModule and all its contents */
|
||||
void cell_module_free (CellModule *mod);
|
||||
|
||||
/* Write a CellModule to a byte buffer.
|
||||
Returns allocated buffer (caller must free with pjs_free), or NULL on error. */
|
||||
uint8_t *cell_module_write (CellModule *mod, size_t *out_len);
|
||||
|
||||
/* Read a CellModule from a byte buffer.
|
||||
Returns allocated CellModule (caller must free with cell_module_free), or NULL on error. */
|
||||
CellModule *cell_module_read (const uint8_t *buf, size_t buf_len);
|
||||
|
||||
/* Convert compiled JSFunctionBytecode to CellModule.
|
||||
Returns allocated CellModule (caller must free with cell_module_free), or NULL on error. */
|
||||
CellModule *cell_module_from_bytecode (JSContext *ctx, JSFunctionBytecode *main_func);
|
||||
|
||||
/* Compile source code directly to CellModule.
|
||||
Returns allocated CellModule (caller must free with cell_module_free), or NULL on error. */
|
||||
CellModule *JS_CompileModule (JSContext *ctx, const char *input, size_t input_len, const char *filename);
|
||||
|
||||
/* Parse source code and return AST as JSON string.
|
||||
Returns malloc'd JSON string (caller must free), or NULL on error.
|
||||
No JSContext needed — pure string transformation. */
|
||||
char *JS_AST (const char *source, size_t len, const char *filename);
|
||||
|
||||
/* Tokenize source code and return token array as JSON string.
|
||||
Returns malloc'd JSON string (caller must free), or NULL on error.
|
||||
No JSContext needed — pure string transformation. */
|
||||
char *JS_Tokenize (const char *source, size_t len, const char *filename);
|
||||
|
||||
/* Compiled bytecode (context-free, serializable) */
|
||||
typedef struct MachCode MachCode;
|
||||
|
||||
/* Compile AST JSON to context-free MachCode.
|
||||
Returns MachCode* (caller must free with JS_FreeMachCode), or NULL on error. */
|
||||
MachCode *JS_CompileMach(const char *ast_json);
|
||||
|
||||
/* Free a compiled MachCode tree. */
|
||||
void JS_FreeMachCode(MachCode *mc);
|
||||
|
||||
/* Load compiled MachCode into a JSContext, materializing JSValues.
|
||||
Returns JSCodeRegister* linked and ready for execution. */
|
||||
struct JSCodeRegister *JS_LoadMachCode(JSContext *ctx, MachCode *mc, JSValue env);
|
||||
|
||||
/* Dump MACH bytecode to stdout for debugging. Takes AST JSON.
|
||||
Internally compiles, loads, and dumps binary bytecode. */
|
||||
void JS_DumpMach (JSContext *ctx, const char *ast_json, JSValue env);
|
||||
|
||||
/* Compile and execute MACH bytecode. Takes AST JSON.
|
||||
Internally compiles, loads, and executes.
|
||||
Returns result of execution, or JS_EXCEPTION on error. */
|
||||
JSValue JS_RunMach (JSContext *ctx, const char *ast_json, JSValue env);
|
||||
|
||||
/* Compile AST JSON to MCODE JSON (string-based IR).
|
||||
Returns malloc'd JSON string, or NULL on error. Caller must free.
|
||||
No JSContext needed — pure string transformation. */
|
||||
char *JS_Mcode (const char *ast_json);
|
||||
|
||||
/* Parse and execute MCODE JSON directly via the MCODE interpreter.
|
||||
Returns result of execution, or JS_EXCEPTION on error. */
|
||||
JSValue JS_CallMcode (JSContext *ctx, const char *mcode_json);
|
||||
|
||||
/* Integrate a CellModule with an environment and execute.
|
||||
Returns callable function value, or JS_EXCEPTION on error. */
|
||||
JSValue cell_module_integrate (JSContext *ctx, CellModule *mod, JSValue env);
|
||||
/* Intrinsic setup functions */
|
||||
void JS_AddIntrinsicBaseObjects (JSContext *ctx);
|
||||
void JS_AddIntrinsicBasicObjects (JSContext *ctx);
|
||||
void JS_AddIntrinsicEval (JSContext *ctx);
|
||||
void JS_AddIntrinsicRegExp (JSContext *ctx);
|
||||
void JS_AddIntrinsicJSON (JSContext *ctx);
|
||||
|
||||
#undef js_unlikely
|
||||
#undef inline
|
||||
|
||||
669
source/suite.c
669
source/suite.c
@@ -5,7 +5,6 @@
|
||||
*/
|
||||
|
||||
#include "quickjs.h"
|
||||
#include "cJSON.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
@@ -955,6 +954,24 @@ TEST(array_foreach_basic) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ============================================================================
|
||||
GLOBAL OBJECT TEST
|
||||
============================================================================ */
|
||||
|
||||
TEST(global_object) {
|
||||
JSGCRef global_ref;
|
||||
JS_PushGCRef(ctx, &global_ref);
|
||||
global_ref.val = JS_GetGlobalObject(ctx);
|
||||
ASSERT(JS_IsRecord(global_ref.val));
|
||||
|
||||
/* Set something on global */
|
||||
JS_SetPropertyStr(ctx, global_ref.val, "testGlobal", JS_NewInt32(ctx, 777));
|
||||
JSValue val = JS_GetPropertyStr(ctx, global_ref.val, "testGlobal");
|
||||
JS_PopGCRef(ctx, &global_ref);
|
||||
ASSERT_INT(val, 777);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ============================================================================
|
||||
VALUE CONVERSION TESTS
|
||||
============================================================================ */
|
||||
@@ -1504,6 +1521,23 @@ TEST(new_cfunction_with_args) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(call_function_on_global) {
|
||||
JSGCRef func_ref, global_ref;
|
||||
JS_PushGCRef(ctx, &global_ref);
|
||||
JS_PushGCRef(ctx, &func_ref);
|
||||
global_ref.val = JS_GetGlobalObject(ctx);
|
||||
func_ref.val = JS_NewCFunction(ctx, cfunc_return_42, "testFunc", 0);
|
||||
JS_SetPropertyStr(ctx, global_ref.val, "testFunc", func_ref.val);
|
||||
JSValue got = JS_GetPropertyStr(ctx, global_ref.val, "testFunc");
|
||||
int is_func = JS_IsFunction(got);
|
||||
JSValue result = JS_Call(ctx, got, JS_NULL, 0, NULL);
|
||||
JS_PopGCRef(ctx, &func_ref);
|
||||
JS_PopGCRef(ctx, &global_ref);
|
||||
ASSERT(is_func);
|
||||
ASSERT_INT(result, 42);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ============================================================================
|
||||
PROPERTY ACCESS TESTS
|
||||
============================================================================ */
|
||||
@@ -1839,580 +1873,6 @@ TEST(is_integer_vs_number) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ============================================================================
|
||||
SERIALIZATION TESTS - JSON, NOTA, WOTA
|
||||
============================================================================ */
|
||||
|
||||
/* stdlib.h provides free() */
|
||||
#include <stdlib.h>
|
||||
|
||||
/* JSON Tests */
|
||||
|
||||
TEST(json_encode_object) {
|
||||
/* Skip - requires GC rooting fixes in JS_JSONStringify */
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(json_decode_object) {
|
||||
/* Test using JS_ParseJSON directly instead of module API */
|
||||
const char *json = "{\"x\":42,\"y\":\"test\"}";
|
||||
JSValue result = JS_ParseJSON(ctx, json, strlen(json), "<test>");
|
||||
|
||||
int is_record = JS_IsRecord(result);
|
||||
JSValue x = JS_GetPropertyStr(ctx, result, "x");
|
||||
JSValue y = JS_GetPropertyStr(ctx, result, "y");
|
||||
|
||||
ASSERT(is_record);
|
||||
ASSERT_INT(x, 42);
|
||||
ASSERT_STR(y, "test");
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(json_roundtrip_array) {
|
||||
/* Skip - requires GC rooting fixes in JS_JSONStringify */
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* NOTA Tests - use C API directly (value2nota/nota2value) */
|
||||
|
||||
void *value2nota(JSContext *ctx, JSValue v);
|
||||
JSValue nota2value(JSContext *ctx, void *nota);
|
||||
|
||||
TEST(nota_encode_int) {
|
||||
void *encoded = value2nota(ctx, JS_NewInt32(ctx, 42));
|
||||
ASSERT(encoded != NULL);
|
||||
JSValue decoded = nota2value(ctx, encoded);
|
||||
free(encoded);
|
||||
ASSERT_INT(decoded, 42);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(nota_roundtrip_object) {
|
||||
/* Skip - requires GC rooting fixes in nota_encode_value */
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(nota_encode_null) {
|
||||
void *encoded = value2nota(ctx, JS_NULL);
|
||||
ASSERT(encoded != NULL);
|
||||
JSValue decoded = nota2value(ctx, encoded);
|
||||
free(encoded);
|
||||
ASSERT_NULL(decoded);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(nota_encode_bool) {
|
||||
void *enc_true = value2nota(ctx, JS_TRUE);
|
||||
ASSERT(enc_true != NULL);
|
||||
JSValue dec_true = nota2value(ctx, enc_true);
|
||||
free(enc_true);
|
||||
|
||||
void *enc_false = value2nota(ctx, JS_FALSE);
|
||||
ASSERT(enc_false != NULL);
|
||||
JSValue dec_false = nota2value(ctx, enc_false);
|
||||
free(enc_false);
|
||||
|
||||
ASSERT_TRUE(dec_true);
|
||||
ASSERT_FALSE(dec_false);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* WOTA Tests - use C API directly (value2wota/wota2value) */
|
||||
|
||||
void *value2wota(JSContext *ctx, JSValue v, JSValue replacer, size_t *bytes);
|
||||
JSValue wota2value(JSContext *ctx, void *wota);
|
||||
|
||||
TEST(wota_encode_int) {
|
||||
size_t bytes;
|
||||
void *encoded = value2wota(ctx, JS_NewInt32(ctx, 42), JS_NULL, &bytes);
|
||||
ASSERT(encoded != NULL);
|
||||
ASSERT(bytes > 0);
|
||||
JSValue decoded = wota2value(ctx, encoded);
|
||||
free(encoded);
|
||||
ASSERT_INT(decoded, 42);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(wota_roundtrip_object) {
|
||||
JSGCRef obj_ref;
|
||||
JS_PushGCRef(ctx, &obj_ref);
|
||||
obj_ref.val = JS_NewObject(ctx);
|
||||
JS_SetPropertyStr(ctx, obj_ref.val, "val", JS_NewInt32(ctx, 999));
|
||||
JS_SetPropertyStr(ctx, obj_ref.val, "name", JS_NewString(ctx, "wota"));
|
||||
|
||||
size_t bytes;
|
||||
void *encoded = value2wota(ctx, obj_ref.val, JS_NULL, &bytes);
|
||||
JS_PopGCRef(ctx, &obj_ref);
|
||||
ASSERT(encoded != NULL);
|
||||
JSValue decoded = wota2value(ctx, encoded);
|
||||
free(encoded);
|
||||
|
||||
int is_record = JS_IsRecord(decoded);
|
||||
JSValue val = JS_GetPropertyStr(ctx, decoded, "val");
|
||||
JSValue name = JS_GetPropertyStr(ctx, decoded, "name");
|
||||
|
||||
ASSERT(is_record);
|
||||
ASSERT_INT(val, 999);
|
||||
ASSERT_STR(name, "wota");
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(wota_encode_nested_array) {
|
||||
JSGCRef arr_ref, inner_ref;
|
||||
JS_PushGCRef(ctx, &arr_ref);
|
||||
JS_PushGCRef(ctx, &inner_ref);
|
||||
arr_ref.val = JS_NewArray(ctx);
|
||||
inner_ref.val = JS_NewArray(ctx);
|
||||
JS_ArrayPush(ctx, &inner_ref.val, JS_NewInt32(ctx, 10));
|
||||
JS_ArrayPush(ctx, &inner_ref.val, JS_NewInt32(ctx, 20));
|
||||
JS_ArrayPush(ctx, &arr_ref.val, JS_NewInt32(ctx, 1));
|
||||
JS_ArrayPush(ctx, &arr_ref.val, inner_ref.val);
|
||||
JS_ArrayPush(ctx, &arr_ref.val, JS_NewInt32(ctx, 3));
|
||||
|
||||
size_t bytes;
|
||||
void *encoded = value2wota(ctx, arr_ref.val, JS_NULL, &bytes);
|
||||
JS_PopGCRef(ctx, &inner_ref);
|
||||
JS_PopGCRef(ctx, &arr_ref);
|
||||
ASSERT(encoded != NULL);
|
||||
JSValue decoded = wota2value(ctx, encoded);
|
||||
free(encoded);
|
||||
|
||||
int is_arr = JS_IsArray(decoded);
|
||||
int64_t len;
|
||||
JS_GetLength(ctx, decoded, &len);
|
||||
JSValue v0 = JS_GetPropertyUint32(ctx, decoded, 0);
|
||||
JSValue v2 = JS_GetPropertyUint32(ctx, decoded, 2);
|
||||
JSValue inner = JS_GetPropertyUint32(ctx, decoded, 1);
|
||||
int inner_is_arr = JS_IsArray(inner);
|
||||
int64_t inner_len;
|
||||
JS_GetLength(ctx, inner, &inner_len);
|
||||
|
||||
ASSERT(is_arr);
|
||||
ASSERT(len == 3);
|
||||
ASSERT_INT(v0, 1);
|
||||
ASSERT_INT(v2, 3);
|
||||
ASSERT(inner_is_arr);
|
||||
ASSERT(inner_len == 2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(wota_encode_blob) {
|
||||
/* Skip blob test - requires js_new_blob_stoned_copy which is in quickjs.c */
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ============================================================================
|
||||
CELL MODULE TESTS - Serialize/Deserialize bytecode
|
||||
============================================================================ */
|
||||
|
||||
TEST(cell_module_compile_basic) {
|
||||
/* Compile simple source to CellModule */
|
||||
const char *source = "1 + 2";
|
||||
CellModule *mod = JS_CompileModule(ctx, source, strlen(source), "<test>");
|
||||
ASSERT_MSG(mod != NULL, "JS_CompileModule returned NULL");
|
||||
|
||||
/* Check module has units */
|
||||
ASSERT_MSG(mod->unit_count > 0, "Module has no units");
|
||||
ASSERT_MSG(mod->units[0].bytecode_len > 0, "Unit has no bytecode");
|
||||
|
||||
cell_module_free(mod);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(cell_module_write_read) {
|
||||
/* Compile, serialize, deserialize */
|
||||
const char *source = "var x = 10; x * 2";
|
||||
CellModule *mod = JS_CompileModule(ctx, source, strlen(source), "<test>");
|
||||
ASSERT_MSG(mod != NULL, "JS_CompileModule returned NULL");
|
||||
|
||||
/* Serialize */
|
||||
size_t len;
|
||||
uint8_t *buf = cell_module_write(mod, &len);
|
||||
ASSERT_MSG(buf != NULL, "cell_module_write returned NULL");
|
||||
ASSERT_MSG(len > 0, "cell_module_write produced empty buffer");
|
||||
|
||||
/* Deserialize */
|
||||
CellModule *mod2 = cell_module_read(buf, len);
|
||||
free(buf);
|
||||
ASSERT_MSG(mod2 != NULL, "cell_module_read returned NULL");
|
||||
|
||||
/* Verify structure matches */
|
||||
ASSERT_MSG(mod2->unit_count == mod->unit_count, "unit_count mismatch");
|
||||
ASSERT_MSG(mod2->string_count == mod->string_count, "string_count mismatch");
|
||||
|
||||
cell_module_free(mod);
|
||||
cell_module_free(mod2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(cell_module_integrate_basic) {
|
||||
/* Compile, then integrate and execute */
|
||||
const char *source = "3 + 4";
|
||||
CellModule *mod = JS_CompileModule(ctx, source, strlen(source), "<test>");
|
||||
ASSERT_MSG(mod != NULL, "JS_CompileModule returned NULL");
|
||||
|
||||
/* Integrate into context */
|
||||
JSValue func = cell_module_integrate(ctx, mod, JS_NULL);
|
||||
if (JS_IsException(func)) {
|
||||
cell_module_free(mod);
|
||||
ASSERT_MSG(0, "cell_module_integrate threw exception");
|
||||
}
|
||||
|
||||
/* Execute */
|
||||
JSValue result = JS_Call(ctx, func, JS_NULL, 0, NULL);
|
||||
JS_FreeValue(ctx, func);
|
||||
cell_module_free(mod);
|
||||
|
||||
if (JS_IsException(result)) {
|
||||
ASSERT_MSG(0, "JS_Call threw exception");
|
||||
}
|
||||
|
||||
ASSERT_INT(result, 7);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(cell_module_roundtrip_execute) {
|
||||
/* Full round-trip: compile -> write -> read -> integrate -> execute */
|
||||
const char *source = "var a = 5; var b = 3; a * b";
|
||||
CellModule *mod = JS_CompileModule(ctx, source, strlen(source), "<test>");
|
||||
ASSERT_MSG(mod != NULL, "JS_CompileModule returned NULL");
|
||||
|
||||
/* Serialize */
|
||||
size_t len;
|
||||
uint8_t *buf = cell_module_write(mod, &len);
|
||||
cell_module_free(mod);
|
||||
ASSERT_MSG(buf != NULL, "cell_module_write returned NULL");
|
||||
|
||||
/* Deserialize */
|
||||
CellModule *mod2 = cell_module_read(buf, len);
|
||||
free(buf);
|
||||
ASSERT_MSG(mod2 != NULL, "cell_module_read returned NULL");
|
||||
|
||||
/* Integrate and execute */
|
||||
JSValue func = cell_module_integrate(ctx, mod2, JS_NULL);
|
||||
cell_module_free(mod2);
|
||||
if (JS_IsException(func)) {
|
||||
ASSERT_MSG(0, "cell_module_integrate threw exception");
|
||||
}
|
||||
|
||||
JSValue result = JS_Call(ctx, func, JS_NULL, 0, NULL);
|
||||
JS_FreeValue(ctx, func);
|
||||
|
||||
if (JS_IsException(result)) {
|
||||
ASSERT_MSG(0, "JS_Call threw exception");
|
||||
}
|
||||
|
||||
ASSERT_INT(result, 15);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(cell_module_string_constant) {
|
||||
/* Test string constant handling */
|
||||
const char *source = "'hello' + ' world'";
|
||||
CellModule *mod = JS_CompileModule(ctx, source, strlen(source), "<test>");
|
||||
ASSERT_MSG(mod != NULL, "JS_CompileModule returned NULL");
|
||||
|
||||
/* Verify string table has entries */
|
||||
ASSERT_MSG(mod->string_count > 0, "Module has no strings");
|
||||
|
||||
/* Integrate and execute */
|
||||
JSValue func = cell_module_integrate(ctx, mod, JS_NULL);
|
||||
cell_module_free(mod);
|
||||
if (JS_IsException(func)) {
|
||||
ASSERT_MSG(0, "cell_module_integrate threw exception");
|
||||
}
|
||||
|
||||
JSValue result = JS_Call(ctx, func, JS_NULL, 0, NULL);
|
||||
JS_FreeValue(ctx, func);
|
||||
|
||||
if (JS_IsException(result)) {
|
||||
ASSERT_MSG(0, "JS_Call threw exception");
|
||||
}
|
||||
|
||||
ASSERT_STR(result, "hello world");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ============================================================================
|
||||
ERROR RECOVERY TESTS - Helper macros
|
||||
============================================================================ */
|
||||
|
||||
#define ASSERT_HAS_ERRORS(json_str, min_count) do { \
|
||||
cJSON *_root = cJSON_Parse(json_str); \
|
||||
ASSERT_MSG(_root != NULL, "failed to parse JSON output"); \
|
||||
cJSON *_errs = cJSON_GetObjectItem(_root, "errors"); \
|
||||
if (!_errs || !cJSON_IsArray(_errs) || cJSON_GetArraySize(_errs) < (min_count)) { \
|
||||
printf("[line %d: expected at least %d error(s), got %d] ", __LINE__, (min_count), \
|
||||
_errs && cJSON_IsArray(_errs) ? cJSON_GetArraySize(_errs) : 0); \
|
||||
cJSON_Delete(_root); \
|
||||
return 0; \
|
||||
} \
|
||||
cJSON_Delete(_root); \
|
||||
} while(0)
|
||||
|
||||
#define ASSERT_NO_ERRORS(json_str) do { \
|
||||
cJSON *_root = cJSON_Parse(json_str); \
|
||||
ASSERT_MSG(_root != NULL, "failed to parse JSON output"); \
|
||||
cJSON *_errs = cJSON_GetObjectItem(_root, "errors"); \
|
||||
if (_errs && cJSON_IsArray(_errs) && cJSON_GetArraySize(_errs) > 0) { \
|
||||
cJSON *_first = cJSON_GetArrayItem(_errs, 0); \
|
||||
const char *_msg = cJSON_GetStringValue(cJSON_GetObjectItem(_first, "message")); \
|
||||
printf("[line %d: expected no errors, got: %s] ", __LINE__, _msg ? _msg : "?"); \
|
||||
cJSON_Delete(_root); \
|
||||
return 0; \
|
||||
} \
|
||||
cJSON_Delete(_root); \
|
||||
} while(0)
|
||||
|
||||
#define ASSERT_ERROR_MSG_CONTAINS(json_str, substring) do { \
|
||||
cJSON *_root = cJSON_Parse(json_str); \
|
||||
ASSERT_MSG(_root != NULL, "failed to parse JSON output"); \
|
||||
cJSON *_errs = cJSON_GetObjectItem(_root, "errors"); \
|
||||
int _found = 0; \
|
||||
if (_errs && cJSON_IsArray(_errs)) { \
|
||||
cJSON *_e; \
|
||||
cJSON_ArrayForEach(_e, _errs) { \
|
||||
const char *_msg = cJSON_GetStringValue(cJSON_GetObjectItem(_e, "message")); \
|
||||
if (_msg && strstr(_msg, (substring))) { _found = 1; break; } \
|
||||
} \
|
||||
} \
|
||||
if (!_found) { \
|
||||
printf("[line %d: no error containing '%s'] ", __LINE__, (substring)); \
|
||||
cJSON_Delete(_root); \
|
||||
return 0; \
|
||||
} \
|
||||
cJSON_Delete(_root); \
|
||||
} while(0)
|
||||
|
||||
/* ============================================================================
|
||||
TOKENIZER ERROR TESTS
|
||||
============================================================================ */
|
||||
|
||||
TEST(tokenize_unterminated_string) {
|
||||
const char *src = "var x = \"hello";
|
||||
char *json = JS_Tokenize(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_Tokenize returned NULL");
|
||||
ASSERT_ERROR_MSG_CONTAINS(json, "unterminated string");
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(tokenize_unterminated_template) {
|
||||
const char *src = "var x = `hello";
|
||||
char *json = JS_Tokenize(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_Tokenize returned NULL");
|
||||
ASSERT_ERROR_MSG_CONTAINS(json, "unterminated template");
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(tokenize_unterminated_block_comment) {
|
||||
const char *src = "var x /* comment";
|
||||
char *json = JS_Tokenize(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_Tokenize returned NULL");
|
||||
ASSERT_ERROR_MSG_CONTAINS(json, "unterminated block comment");
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(tokenize_malformed_hex) {
|
||||
const char *src = "var x = 0x";
|
||||
char *json = JS_Tokenize(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_Tokenize returned NULL");
|
||||
ASSERT_ERROR_MSG_CONTAINS(json, "malformed hex");
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(tokenize_malformed_binary) {
|
||||
const char *src = "var x = 0b";
|
||||
char *json = JS_Tokenize(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_Tokenize returned NULL");
|
||||
ASSERT_ERROR_MSG_CONTAINS(json, "malformed binary");
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(tokenize_malformed_exponent) {
|
||||
const char *src = "var x = 1e+";
|
||||
char *json = JS_Tokenize(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_Tokenize returned NULL");
|
||||
ASSERT_ERROR_MSG_CONTAINS(json, "no digits after exponent");
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(tokenize_valid_no_errors) {
|
||||
const char *src = "var x = 42";
|
||||
char *json = JS_Tokenize(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_Tokenize returned NULL");
|
||||
ASSERT_NO_ERRORS(json);
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ============================================================================
|
||||
PARSER ERROR TESTS
|
||||
============================================================================ */
|
||||
|
||||
TEST(ast_missing_identifier_after_var) {
|
||||
const char *src = "var = 1";
|
||||
char *json = JS_AST(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_AST returned NULL");
|
||||
ASSERT_ERROR_MSG_CONTAINS(json, "expected identifier");
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(ast_missing_initializer_def) {
|
||||
const char *src = "def x";
|
||||
char *json = JS_AST(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_AST returned NULL");
|
||||
ASSERT_ERROR_MSG_CONTAINS(json, "missing initializer");
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(ast_recovery_continues_after_error) {
|
||||
const char *src = "var = 1; var y = 2";
|
||||
char *json = JS_AST(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_AST returned NULL");
|
||||
ASSERT_HAS_ERRORS(json, 1);
|
||||
/* Check that 'y' statement is present in the AST */
|
||||
ASSERT_MSG(strstr(json, "\"y\"") != NULL, "recovery failed: 'y' not in AST");
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(ast_valid_no_errors) {
|
||||
const char *src = "var x = 1; var y = 2";
|
||||
char *json = JS_AST(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_AST returned NULL");
|
||||
ASSERT_NO_ERRORS(json);
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ============================================================================
|
||||
AST SEMANTIC ERROR TESTS
|
||||
============================================================================ */
|
||||
|
||||
TEST(ast_sem_assign_to_const) {
|
||||
const char *src = "def x = 5; x = 3";
|
||||
char *json = JS_AST(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_AST returned NULL");
|
||||
ASSERT_ERROR_MSG_CONTAINS(json, "cannot assign to constant");
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(ast_sem_assign_to_arg) {
|
||||
const char *src = "function(x) { x = 5; }";
|
||||
char *json = JS_AST(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_AST returned NULL");
|
||||
ASSERT_ERROR_MSG_CONTAINS(json, "cannot assign to constant");
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(ast_sem_redeclare_const) {
|
||||
const char *src = "def x = 1; def x = 2";
|
||||
char *json = JS_AST(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_AST returned NULL");
|
||||
ASSERT_ERROR_MSG_CONTAINS(json, "cannot redeclare constant");
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(ast_sem_break_outside_loop) {
|
||||
const char *src = "break";
|
||||
char *json = JS_AST(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_AST returned NULL");
|
||||
ASSERT_ERROR_MSG_CONTAINS(json, "outside of loop");
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(ast_sem_continue_outside_loop) {
|
||||
const char *src = "continue";
|
||||
char *json = JS_AST(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_AST returned NULL");
|
||||
ASSERT_ERROR_MSG_CONTAINS(json, "outside of loop");
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(ast_sem_break_inside_loop_ok) {
|
||||
const char *src = "while (true) { break; }";
|
||||
char *json = JS_AST(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_AST returned NULL");
|
||||
ASSERT_NO_ERRORS(json);
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(ast_sem_increment_const) {
|
||||
const char *src = "def x = 1; x++";
|
||||
char *json = JS_AST(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_AST returned NULL");
|
||||
ASSERT_ERROR_MSG_CONTAINS(json, "cannot assign to constant");
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(ast_sem_shadow_var_ok) {
|
||||
const char *src = "var array = []; array";
|
||||
char *json = JS_AST(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_AST returned NULL");
|
||||
ASSERT_NO_ERRORS(json);
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(ast_sem_var_assign_ok) {
|
||||
const char *src = "var x = 1; x = x + 1";
|
||||
char *json = JS_AST(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_AST returned NULL");
|
||||
ASSERT_NO_ERRORS(json);
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(ast_sem_nested_function_scope) {
|
||||
const char *src = "var x = 1; function f(x) { return x + 1; }";
|
||||
char *json = JS_AST(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(json != NULL, "JS_AST returned NULL");
|
||||
ASSERT_NO_ERRORS(json);
|
||||
free(json);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ============================================================================
|
||||
CODEGEN TESTS (updated for new direct AST-to-bytecode compiler)
|
||||
============================================================================ */
|
||||
|
||||
TEST(mach_compile_basic) {
|
||||
const char *src = "var x = 1; x = x + 1";
|
||||
char *ast_json = JS_AST(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(ast_json != NULL, "JS_AST returned NULL");
|
||||
MachCode *mc = JS_CompileMach(ast_json);
|
||||
free(ast_json);
|
||||
ASSERT_MSG(mc != NULL, "JS_CompileMach returned NULL");
|
||||
JS_FreeMachCode(mc);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST(mach_compile_function) {
|
||||
const char *src = "function f(x) { return x + 1 }";
|
||||
char *ast_json = JS_AST(src, strlen(src), "<test>");
|
||||
ASSERT_MSG(ast_json != NULL, "JS_AST returned NULL");
|
||||
MachCode *mc = JS_CompileMach(ast_json);
|
||||
free(ast_json);
|
||||
ASSERT_MSG(mc != NULL, "JS_CompileMach returned NULL");
|
||||
JS_FreeMachCode(mc);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ============================================================================
|
||||
MAIN TEST RUNNER
|
||||
============================================================================ */
|
||||
@@ -2491,6 +1951,9 @@ int run_c_test_suite(JSContext *ctx)
|
||||
RUN_TEST(strict_eq_null);
|
||||
RUN_TEST(strict_eq_bool);
|
||||
|
||||
printf("\nGlobal Object:\n");
|
||||
RUN_TEST(global_object);
|
||||
|
||||
printf("\nValue Conversions:\n");
|
||||
RUN_TEST(to_bool_true_values);
|
||||
RUN_TEST(to_bool_false_values);
|
||||
@@ -2563,6 +2026,7 @@ int run_c_test_suite(JSContext *ctx)
|
||||
printf("\nC Functions:\n");
|
||||
RUN_TEST(new_cfunction_no_args);
|
||||
RUN_TEST(new_cfunction_with_args);
|
||||
RUN_TEST(call_function_on_global);
|
||||
|
||||
printf("\nProperty Access:\n");
|
||||
RUN_TEST(get_property_with_jsvalue_key);
|
||||
@@ -2605,61 +2069,6 @@ int run_c_test_suite(JSContext *ctx)
|
||||
RUN_TEST(is_function_check);
|
||||
RUN_TEST(is_integer_vs_number);
|
||||
|
||||
printf("\nSerialization - JSON:\n");
|
||||
RUN_TEST(json_encode_object);
|
||||
RUN_TEST(json_decode_object);
|
||||
RUN_TEST(json_roundtrip_array);
|
||||
|
||||
printf("\nSerialization - NOTA:\n");
|
||||
RUN_TEST(nota_encode_int);
|
||||
RUN_TEST(nota_roundtrip_object);
|
||||
RUN_TEST(nota_encode_null);
|
||||
RUN_TEST(nota_encode_bool);
|
||||
|
||||
printf("\nSerialization - WOTA:\n");
|
||||
RUN_TEST(wota_encode_int);
|
||||
RUN_TEST(wota_roundtrip_object);
|
||||
RUN_TEST(wota_encode_nested_array);
|
||||
RUN_TEST(wota_encode_blob);
|
||||
|
||||
// CellModule tests
|
||||
RUN_TEST(cell_module_compile_basic);
|
||||
RUN_TEST(cell_module_write_read);
|
||||
RUN_TEST(cell_module_integrate_basic);
|
||||
RUN_TEST(cell_module_roundtrip_execute);
|
||||
RUN_TEST(cell_module_string_constant);
|
||||
|
||||
printf("\nTokenizer Errors:\n");
|
||||
RUN_TEST(tokenize_unterminated_string);
|
||||
RUN_TEST(tokenize_unterminated_template);
|
||||
RUN_TEST(tokenize_unterminated_block_comment);
|
||||
RUN_TEST(tokenize_malformed_hex);
|
||||
RUN_TEST(tokenize_malformed_binary);
|
||||
RUN_TEST(tokenize_malformed_exponent);
|
||||
RUN_TEST(tokenize_valid_no_errors);
|
||||
|
||||
printf("\nParser Errors:\n");
|
||||
RUN_TEST(ast_missing_identifier_after_var);
|
||||
RUN_TEST(ast_missing_initializer_def);
|
||||
RUN_TEST(ast_recovery_continues_after_error);
|
||||
RUN_TEST(ast_valid_no_errors);
|
||||
|
||||
printf("\nAST Semantic Errors:\n");
|
||||
RUN_TEST(ast_sem_assign_to_const);
|
||||
RUN_TEST(ast_sem_assign_to_arg);
|
||||
RUN_TEST(ast_sem_redeclare_const);
|
||||
RUN_TEST(ast_sem_break_outside_loop);
|
||||
RUN_TEST(ast_sem_continue_outside_loop);
|
||||
RUN_TEST(ast_sem_break_inside_loop_ok);
|
||||
RUN_TEST(ast_sem_increment_const);
|
||||
RUN_TEST(ast_sem_shadow_var_ok);
|
||||
RUN_TEST(ast_sem_var_assign_ok);
|
||||
RUN_TEST(ast_sem_nested_function_scope);
|
||||
|
||||
printf("\nCodegen:\n");
|
||||
RUN_TEST(mach_compile_basic);
|
||||
RUN_TEST(mach_compile_function);
|
||||
|
||||
printf("\n=================================\n");
|
||||
printf("Results: %d passed, %d failed\n", tests_passed, tests_failed);
|
||||
printf("=================================\n\n");
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
var f = x => { return x }; f(1)
|
||||
@@ -1 +0,0 @@
|
||||
var f = (x = 10) => x; f()
|
||||
@@ -1 +0,0 @@
|
||||
var f = x => x * 2; f(5)
|
||||
@@ -1 +0,0 @@
|
||||
var f = (a, b) => a + b; f(2, 3)
|
||||
@@ -1 +0,0 @@
|
||||
var f = () => 42; f()
|
||||
@@ -1 +0,0 @@
|
||||
var x = 5; x += 3; x
|
||||
@@ -1 +0,0 @@
|
||||
var x = 7; x &= 3; x
|
||||
@@ -1 +0,0 @@
|
||||
var x = 6; x /= 2; x
|
||||
@@ -1 +0,0 @@
|
||||
var x = 5; x &&= 10; x
|
||||
@@ -1 +0,0 @@
|
||||
var x = 0; x ||= 10; x
|
||||
@@ -1 +0,0 @@
|
||||
var x = 7; x %= 3; x
|
||||
@@ -1 +0,0 @@
|
||||
var x = 5; x *= 3; x
|
||||
@@ -1 +0,0 @@
|
||||
var x = null; x ??= 10; x
|
||||
@@ -1 +0,0 @@
|
||||
var x = 5; x |= 2; x
|
||||
@@ -1 +0,0 @@
|
||||
var x = 2; x **= 3; x
|
||||
@@ -1 +0,0 @@
|
||||
var x = 2; x <<= 3; x
|
||||
@@ -1 +0,0 @@
|
||||
var x = 8; x >>= 2; x
|
||||
@@ -1 +0,0 @@
|
||||
var x = -8; x >>>= 2; x
|
||||
@@ -1 +0,0 @@
|
||||
var x = 5; x -= 3; x
|
||||
@@ -1 +0,0 @@
|
||||
var x = 5; x ^= 3; x
|
||||
@@ -1 +0,0 @@
|
||||
var x, y; x = y = 5; x + y
|
||||
@@ -1 +0,0 @@
|
||||
var f = function(x) { return function() { return x } }; f(5)()
|
||||
@@ -1,11 +0,0 @@
|
||||
var counter = function() {
|
||||
var n = 0
|
||||
return function() {
|
||||
n = n + 1
|
||||
return n
|
||||
}
|
||||
}
|
||||
var c = counter()
|
||||
c()
|
||||
c()
|
||||
c()
|
||||
@@ -1,3 +0,0 @@
|
||||
// simple test that comments work
|
||||
var x = 5
|
||||
// other comment
|
||||
@@ -1 +0,0 @@
|
||||
/* comment */ 5
|
||||
@@ -1 +0,0 @@
|
||||
1 /* a */ + /* b */ 2
|
||||
@@ -1 +0,0 @@
|
||||
def x = 5; x
|
||||
@@ -1 +0,0 @@
|
||||
var i = 0; do { i = i + 1 } while (i < 3); i
|
||||
@@ -1 +0,0 @@
|
||||
var s = 0; var i = 0; do { i = i + 1; if (i == 2) continue; s = s + i } while (i < 5); s
|
||||
@@ -1 +0,0 @@
|
||||
;;; 5
|
||||
@@ -1 +0,0 @@
|
||||
var s = 0; for (var i = 0; i < 3; i++) s = s + i; s
|
||||
@@ -1 +0,0 @@
|
||||
var s = 0; for (var i = 0; i < 10; i++) { if (i == 4) break; s = s + i }; s
|
||||
@@ -1 +0,0 @@
|
||||
var s = 0; for (var i = 0; i < 5; i++) { if (i == 2) continue; s = s + i }; s
|
||||
@@ -1 +0,0 @@
|
||||
var f = function(x) { return x * 2 }; f(3)
|
||||
@@ -1 +0,0 @@
|
||||
(function(x) { return x * 2 })(5)
|
||||
@@ -1 +0,0 @@
|
||||
function fac(n) { if (n <= 1) return 1; return n * fac(n - 1) }; fac(5)
|
||||
@@ -1,2 +0,0 @@
|
||||
function a() { go b() }
|
||||
function b() { 1 }
|
||||
@@ -1,2 +0,0 @@
|
||||
var o = {m: function() { 1 }}
|
||||
function f() { go o.m() }
|
||||
@@ -1 +0,0 @@
|
||||
var x = 0; if (true) x = 1; x
|
||||
@@ -1 +0,0 @@
|
||||
if (false) 1 else 2
|
||||
@@ -1 +0,0 @@
|
||||
print("a")
|
||||
@@ -1 +0,0 @@
|
||||
var s = 0; outer: for (var i = 0; i < 3; i++) { for (var j = 0; j < 3; j++) { if (j == 1) continue outer; s = s + 1 } }; s
|
||||
@@ -1 +0,0 @@
|
||||
var x = 1, y = 2; x + y
|
||||
@@ -1 +0,0 @@
|
||||
var x = 1; { var y = 2; { var z = 3; x = x + y + z } }; x
|
||||
@@ -1 +0,0 @@
|
||||
0b1010
|
||||
@@ -1 +0,0 @@
|
||||
1e3
|
||||
@@ -1 +0,0 @@
|
||||
3.14
|
||||
@@ -1 +0,0 @@
|
||||
0xff
|
||||
@@ -1 +0,0 @@
|
||||
0o17
|
||||
@@ -1 +0,0 @@
|
||||
1_000_000
|
||||
@@ -1 +0,0 @@
|
||||
1 + 2 * 3
|
||||
@@ -1 +0,0 @@
|
||||
5 & 3
|
||||
@@ -1 +0,0 @@
|
||||
~5
|
||||
@@ -1 +0,0 @@
|
||||
5 | 2
|
||||
@@ -1 +0,0 @@
|
||||
5 ^ 3
|
||||
@@ -1 +0,0 @@
|
||||
(1, 2, 3)
|
||||
@@ -1 +0,0 @@
|
||||
5 > 3
|
||||
@@ -1 +0,0 @@
|
||||
3 == 3
|
||||
@@ -1 +0,0 @@
|
||||
5 >= 5
|
||||
@@ -1 +0,0 @@
|
||||
3 < 5
|
||||
@@ -1 +0,0 @@
|
||||
3 <= 3
|
||||
@@ -1 +0,0 @@
|
||||
3 != 4
|
||||
@@ -1 +0,0 @@
|
||||
var x = 5; x--; x
|
||||
@@ -1 +0,0 @@
|
||||
var x = 5; --x
|
||||
@@ -1 +0,0 @@
|
||||
var o = {x: 1}; delete o.x; o.x
|
||||
@@ -1 +0,0 @@
|
||||
var o = {x: 1}; "x" in o
|
||||
@@ -1 +0,0 @@
|
||||
var x = 5; x++; x
|
||||
@@ -1 +0,0 @@
|
||||
var x = 5; ++x
|
||||
@@ -1 +0,0 @@
|
||||
true && false
|
||||
@@ -1 +0,0 @@
|
||||
!false
|
||||
@@ -1 +0,0 @@
|
||||
false || true
|
||||
@@ -1 +0,0 @@
|
||||
null ?? 5
|
||||
@@ -1 +0,0 @@
|
||||
2 ** 3
|
||||
@@ -1 +0,0 @@
|
||||
2 << 3
|
||||
@@ -1 +0,0 @@
|
||||
8 >> 2
|
||||
@@ -1 +0,0 @@
|
||||
-8 >>> 2
|
||||
@@ -1 +0,0 @@
|
||||
true ? 1 : 2
|
||||
@@ -1 +0,0 @@
|
||||
-5
|
||||
@@ -1 +0,0 @@
|
||||
+"5"
|
||||
@@ -1 +0,0 @@
|
||||
var o = {a: 1}; o?.["a"]
|
||||
@@ -1 +0,0 @@
|
||||
var o = {f: () => 1}; o.f?.()
|
||||
@@ -1 +0,0 @@
|
||||
var o = null; o?.a
|
||||
@@ -1 +0,0 @@
|
||||
var o = {a: 1}; o?.a
|
||||
@@ -1 +0,0 @@
|
||||
(1 + 2) * 3
|
||||
@@ -1 +0,0 @@
|
||||
var a = {x: 1}; a["x"]
|
||||
@@ -1 +0,0 @@
|
||||
var o = {a: {b: {c: 1}}}; o.a.b.c
|
||||
@@ -1 +0,0 @@
|
||||
var k = "x"; var a = {x: 1}; a[k]
|
||||
@@ -1 +0,0 @@
|
||||
var a = {x: 1}; a.x
|
||||
@@ -1 +0,0 @@
|
||||
var o = {a: {b: 1}}; o.a.b
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user