2 Commits

Author SHA1 Message Date
John Alanbrook
cbd27e0783 ffi module 2025-06-23 04:57:05 -05:00
John Alanbrook
6eb33b8e48 remove unneeded object properties and if 0 code 2025-06-19 08:46:40 -05:00
6 changed files with 430 additions and 384 deletions

View File

@@ -1,5 +1,7 @@
JAVASCRIPT VISION
I see objects as being a sort of combination of a lisp cell and a record: symbols, which are used internally, and are private and non iterable, and record string values, which are iterable, readable, and writable; of course everything becomes locked in when stone.
CELLSCRIPT
Javascript to its core. Objects. What does the language need? It can be quite small, I think. The key is, ANYTHING that we want to be fast and JIT'd, must be present. So, record lookups. These are actually quicker in a jit'd language that have them as a feature. Most things should be libraries. Blobs need to be in the runtime.

View File

@@ -190,6 +190,8 @@ else
deps += chipmunk_dep
endif
deps += dependency('libffi', static:true)
if host_machine.system() != 'emscripten'
# Try to find system-installed enet first
enet_dep = dependency('enet', static: true, required: false)
@@ -271,7 +273,7 @@ src += [
'anim.c', 'config.c', 'datastream.c','font.c','HandmadeMath.c','jsffi.c','model.c',
'render.c','simplex.c','spline.c', 'transform.c','cell.c', 'wildmatch.c',
'sprite.c', 'rtree.c', 'qjs_nota.c', 'qjs_soloud.c', 'qjs_sdl.c', 'qjs_sdl_input.c', 'qjs_sdl_video.c', 'qjs_sdl_surface.c', 'qjs_math.c', 'qjs_geometry.c', 'qjs_transform.c', 'qjs_sprite.c', 'qjs_io.c', 'qjs_fd.c', 'qjs_os.c', 'qjs_actor.c',
'qjs_qr.c', 'qjs_wota.c', 'monocypher.c', 'qjs_blob.c', 'qjs_crypto.c', 'qjs_time.c', 'qjs_http.c', 'qjs_rtree.c', 'qjs_spline.c', 'qjs_js.c', 'qjs_debug.c', 'picohttpparser.c', 'qjs_miniz.c', 'qjs_num.c', 'timer.c', 'qjs_socket.c', 'qjs_kim.c', 'qjs_utf8.c', 'qjs_fit.c', 'qjs_text.c'
'qjs_qr.c', 'qjs_wota.c', 'monocypher.c', 'qjs_blob.c', 'qjs_crypto.c', 'qjs_time.c', 'qjs_http.c', 'qjs_rtree.c', 'qjs_spline.c', 'qjs_js.c', 'qjs_debug.c', 'picohttpparser.c', 'qjs_miniz.c', 'qjs_num.c', 'timer.c', 'qjs_socket.c', 'qjs_kim.c', 'qjs_utf8.c', 'qjs_fit.c', 'qjs_text.c', 'point.c', 'qjs_ffi.c'
]
# quirc src
src += [

View File

@@ -1546,6 +1546,7 @@ JSC_CCALL(os_value_id,
#include "qjs_wota.h"
#include "qjs_socket.h"
#include "qjs_nota.h"
#include "qjs_ffi.h"
//JSValue js_imgui_use(JSContext *js);
#define MISTLINE(NAME) (ModuleEntry){#NAME, js_##NAME##_use}
@@ -1581,6 +1582,7 @@ void ffi_load(JSContext *js)
arrput(rt->module_registry, MISTLINE(text));
arrput(rt->module_registry, MISTLINE(wota));
arrput(rt->module_registry, MISTLINE(nota));
arrput(rt->module_registry, MISTLINE(ffi));
// power user
arrput(rt->module_registry, MISTLINE(js));

413
source/qjs_ffi.c Normal file
View File

@@ -0,0 +1,413 @@
/*
* qjs_ffi.c QuickJS ↔ libffi bridge
*
* Works on macOS / Linux / Windows. See examples/ffi_test.js.
*/
#include "qjs_ffi.h"
#include <ffi.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#ifndef countof
#define countof(x) (sizeof(x) / sizeof((x)[0]))
#endif
/* -------------------------------------------------------------------------- */
/* internal structs */
/* -------------------------------------------------------------------------- */
typedef struct {
ffi_cif cif;
ffi_type **arg_types;
ffi_type *ret_type;
void *fn_ptr;
int nargs;
} ffi_func;
typedef struct {
void *ptr;
int is_library; /* 1 => real dlopen/LoadLibrary handle */
} ffi_pointer;
/* -------------------------------------------------------------------------- */
/* QuickJS class plumbing */
/* -------------------------------------------------------------------------- */
static JSClassID js_ffi_func_id;
static JSClassID js_ffi_pointer_id;
static void ffi_func_free(ffi_func *f)
{
if (!f) return;
if (f->arg_types) {
for (int i = 0; i < f->nargs; i++) {
ffi_type *t = f->arg_types[i];
if (t && t != &ffi_type_void && t != &ffi_type_pointer &&
t != &ffi_type_sint8 && t != &ffi_type_uint8 &&
t != &ffi_type_sint16 && t != &ffi_type_uint16 &&
t != &ffi_type_sint32 && t != &ffi_type_uint32 &&
t != &ffi_type_sint64 && t != &ffi_type_uint64 &&
t != &ffi_type_float && t != &ffi_type_double)
free(t);
}
free(f->arg_types);
}
ffi_type *rtp = f->ret_type;
if (rtp && rtp != &ffi_type_void && rtp != &ffi_type_pointer &&
rtp != &ffi_type_sint8 && rtp != &ffi_type_uint8 &&
rtp != &ffi_type_sint16 && rtp != &ffi_type_uint16 &&
rtp != &ffi_type_sint32 && rtp != &ffi_type_uint32 &&
rtp != &ffi_type_sint64 && rtp != &ffi_type_uint64 &&
rtp != &ffi_type_float && rtp != &ffi_type_double)
free(rtp);
free(f);
}
static void ffi_func_finalizer(JSRuntime *rt, JSValue val)
{
ffi_func *f = JS_GetOpaque(val, js_ffi_func_id);
if (f) ffi_func_free(f);
}
static void ffi_pointer_free(ffi_pointer *p)
{
if (p->is_library && p->ptr) {
#ifdef _WIN32
FreeLibrary((HMODULE)p->ptr);
#else
dlclose(p->ptr);
#endif
}
free(p);
}
static void ffi_pointer_finalizer(JSRuntime *rt, JSValue val)
{
ffi_pointer *f = JS_GetOpaque(val, js_ffi_pointer_id);
if (!f) return;
ffi_pointer_free(f);
}
static JSClassDef js_ffi_func_class = { "FFIFunction", .finalizer = ffi_func_finalizer };
static JSClassDef js_ffi_pointer_class = { "FFIPointer", .finalizer = ffi_pointer_finalizer };
/* -------------------------------------------------------------------------- */
/* helpers */
/* -------------------------------------------------------------------------- */
static JSValue
js_new_ffi_pointer(JSContext *ctx, void *ptr, int is_library)
{
JSValue obj = JS_NewObjectClass(ctx, js_ffi_pointer_id);
if (JS_IsException(obj)) return obj;
ffi_pointer *p = malloc(sizeof *p);
if (!p) { JS_FreeValue(ctx, obj); return JS_ThrowOutOfMemory(ctx); }
p->ptr = ptr;
p->is_library = is_library;
JS_SetOpaque(obj, p);
return obj;
}
static void *
js_get_ffi_pointer(JSContext *ctx, JSValueConst val)
{
ffi_pointer *p = JS_GetOpaque2(ctx, val, js_ffi_pointer_id);
return p ? p->ptr : NULL;
}
static ffi_type *
js_to_ffi_type(JSContext *ctx, const char *name)
{
if (!strcmp(name, "void")) return &ffi_type_void;
if (!strcmp(name, "pointer")) return &ffi_type_pointer;
if (!strcmp(name, "float")) return &ffi_type_float;
if (!strcmp(name, "double")) return &ffi_type_double;
if (!strcmp(name, "int8")) return &ffi_type_sint8;
if (!strcmp(name, "uint8")) return &ffi_type_uint8;
if (!strcmp(name, "int16")) return &ffi_type_sint16;
if (!strcmp(name, "uint16")) return &ffi_type_uint16;
if (!strcmp(name, "int32")) return &ffi_type_sint32;
if (!strcmp(name, "uint32")) return &ffi_type_uint32;
if (!strcmp(name, "int64")) return &ffi_type_sint64;
if (!strcmp(name, "uint64")) return &ffi_type_uint64;
/* synonyms */
if (!strcmp(name, "int")) return &ffi_type_sint32;
if (!strcmp(name, "uint")) return &ffi_type_uint32;
if (!strcmp(name, "char*") || !strcmp(name, "string"))
return &ffi_type_pointer;
JS_ThrowTypeError(ctx, "unknown FFI type \"%s\"", name);
return NULL;
}
/* -------------------------------------------------------------------------- */
/* ffi.dlopen */
/* -------------------------------------------------------------------------- */
static JSValue
js_ffi_dlopen(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
if (argc < 1)
return JS_ThrowTypeError(ctx, "dlopen: expected path or null");
const char *path = NULL;
if (!JS_IsNull(argv[0])) {
path = JS_ToCString(ctx, argv[0]);
if (!path) return JS_EXCEPTION;
}
void *h;
#ifdef _WIN32
h = path ? (void *)LoadLibraryA(path)
: (void *)GetModuleHandle(NULL);
#else
h = path ? dlopen(path, RTLD_LAZY | RTLD_LOCAL)
: dlopen(NULL, RTLD_LAZY | RTLD_LOCAL);
#endif
if (path) JS_FreeCString(ctx, path);
if (!h) return JS_ThrowInternalError(ctx, "dlopen failed");
/* ------------- FIX: only real libraries get is_library = 1 ------------- */
int is_library = (path != NULL); /* self handle must not be closed */
return js_new_ffi_pointer(ctx, h, is_library);
}
/* -------------------------------------------------------------------------- */
/* ffi.prepare (unchanged from previous answer) */
/* -------------------------------------------------------------------------- */
static JSValue
js_ffi_prepare(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
if (argc < 4)
return JS_ThrowTypeError(ctx,
"prepare: expected (lib, symbol, ret_type, arg_type_array)");
void *lib = js_get_ffi_pointer(ctx, argv[0]);
if (!lib) return JS_ThrowTypeError(ctx, "invalid library handle");
const char *sym = JS_ToCString(ctx, argv[1]);
if (!sym) return JS_EXCEPTION;
void *fn;
#ifdef _WIN32
fn = (void *)GetProcAddress((HMODULE)lib, sym);
#else
fn = dlsym(lib, sym);
#endif
if (!fn) {
JS_FreeCString(ctx, sym);
return JS_ThrowTypeError(ctx, "symbol \"%s\" not found", sym);
}
const char *rt_name = JS_ToCString(ctx, argv[2]);
if (!rt_name) { JS_FreeCString(ctx, sym); return JS_EXCEPTION; }
ffi_type *ret_type = js_to_ffi_type(ctx, rt_name);
JS_FreeCString(ctx, rt_name);
if (!ret_type) { JS_FreeCString(ctx, sym); return JS_EXCEPTION; }
if (!JS_IsArray(ctx, argv[3])) {
JS_FreeCString(ctx, sym);
return JS_ThrowTypeError(ctx, "arg_types must be an array");
}
uint32_t nargs = JS_ArrayLength(ctx, argv[3]);
ffi_func *f = calloc(1, sizeof *f);
if (!f) { JS_FreeCString(ctx, sym); return JS_ThrowOutOfMemory(ctx); }
f->fn_ptr = fn;
f->ret_type = ret_type;
f->nargs = (int)nargs;
if (nargs) {
f->arg_types = calloc(nargs, sizeof(ffi_type *));
if (!f->arg_types) {
JS_FreeCString(ctx, sym);
ffi_func_free(f);
return JS_ThrowOutOfMemory(ctx);
}
for (uint32_t i = 0; i < nargs; i++) {
JSValue v = JS_GetPropertyUint32(ctx, argv[3], i);
const char *tn = JS_ToCString(ctx, v);
JS_FreeValue(ctx, v);
if (!tn) { JS_FreeCString(ctx, sym); ffi_func_free(f); return JS_EXCEPTION; }
f->arg_types[i] = js_to_ffi_type(ctx, tn);
JS_FreeCString(ctx, tn);
if (!f->arg_types[i]) {
JS_FreeCString(ctx, sym);
ffi_func_free(f);
return JS_EXCEPTION;
}
}
}
JS_FreeCString(ctx, sym);
if (ffi_prep_cif(&f->cif, FFI_DEFAULT_ABI,
f->nargs, f->ret_type, f->arg_types) != FFI_OK) {
ffi_func_free(f);
return JS_ThrowInternalError(ctx, "ffi_prep_cif failed");
}
JSValue obj = JS_NewObjectClass(ctx, js_ffi_func_id);
JS_SetOpaque(obj, f);
return obj;
}
/* -------------------------------------------------------------------------- */
/* box_result + js_ffi_call (unchanged) */
/* -------------------------------------------------------------------------- */
static JSValue
box_result(JSContext *ctx, ffi_type *t, void *data)
{
if (t == &ffi_type_void) return JS_NULL;
if (t == &ffi_type_pointer) return data ? js_new_ffi_pointer(ctx, *(void **)data, 0)
: JS_NULL;
if (t == &ffi_type_double) return JS_NewFloat64(ctx, *(double *)data);
if (t == &ffi_type_float) return JS_NewFloat64(ctx, (double)*(float *)data);
if (t == &ffi_type_sint8) return JS_NewInt32(ctx, *(int8_t *)data);
if (t == &ffi_type_uint8) return JS_NewUint32(ctx, *(uint8_t *)data);
if (t == &ffi_type_sint16) return JS_NewInt32(ctx, *(int16_t *)data);
if (t == &ffi_type_uint16) return JS_NewUint32(ctx, *(uint16_t *)data);
if (t == &ffi_type_sint32) return JS_NewInt32(ctx, *(int32_t *)data);
if (t == &ffi_type_uint32) return JS_NewUint32(ctx, *(uint32_t *)data);
if (t == &ffi_type_sint64) return JS_NewFloat64(ctx, (double)*(int64_t *)data);
if (t == &ffi_type_uint64) return JS_NewFloat64(ctx, (double)*(uint64_t *)data);
return JS_ThrowTypeError(ctx, "unsupported return type");
}
static JSValue
js_ffi_call(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
ffi_func *f = JS_GetOpaque2(ctx, this_val, js_ffi_func_id);
if (!f) return JS_EXCEPTION;
if (argc != f->nargs)
return JS_ThrowTypeError(ctx, "expected %d arguments, got %d",
f->nargs, argc);
/* one fixed stack block per argument ----------------------------------- */
typedef union {
double d;
float f;
int8_t i8; uint8_t u8;
int16_t i16; uint16_t u16;
int32_t i32; uint32_t u32;
int64_t i64; uint64_t u64;
void *p;
} arg_data_t;
arg_data_t data[f->nargs];
void *values[f->nargs];
const char *tmp_cstr[f->nargs]; /* only for JS strings */
memset(tmp_cstr, 0, sizeof tmp_cstr);
/* ---------- marshal JS → C (no malloc for scalars) ------------------- */
for (int i = 0; i < f->nargs; i++) {
ffi_type *t = f->arg_types[i];
/* numbers ------------------------------------------------------------ */
if (t != &ffi_type_pointer) {
double d;
if (JS_ToFloat64(ctx, &d, argv[i]))
goto arg_error;
if (t == &ffi_type_double) data[i].d = d;
else if (t == &ffi_type_float) data[i].f = (float)d;
else if (t == &ffi_type_sint8) data[i].i8 = (int8_t)d;
else if (t == &ffi_type_uint8) data[i].u8 = (uint8_t)d;
else if (t == &ffi_type_sint16) data[i].i16= (int16_t)d;
else if (t == &ffi_type_uint16) data[i].u16= (uint16_t)d;
else if (t == &ffi_type_sint32) data[i].i32= (int32_t)d;
else if (t == &ffi_type_uint32) data[i].u32= (uint32_t)d;
else if (t == &ffi_type_sint64) data[i].i64= (int64_t)d;
else if (t == &ffi_type_uint64) data[i].u64= (uint64_t)d;
else goto arg_error; /* should be unreachable */
values[i] = &data[i];
continue;
}
/* pointers ----------------------------------------------------------- */
if (JS_IsString(argv[i])) {
const char *s = JS_ToCString(ctx, argv[i]);
if (!s) goto arg_error;
tmp_cstr[i] = s; /* remember to free */
data[i].p = (void *)s;
} else {
data[i].p = js_get_ffi_pointer(ctx, argv[i]);
}
values[i] = &data[i];
}
/* ---------------------------- call ---------------------------------- */
uint8_t ret_space[16] = {0};
void *ret_buf = f->ret_type == &ffi_type_void ? NULL : (void *)ret_space;
ffi_call(&f->cif, FFI_FN(f->fn_ptr), ret_buf, values);
JSValue js_ret = box_result(ctx, f->ret_type, ret_buf);
/* --------------------------- cleanup ---------------------------------- */
for (int i = 0; i < f->nargs; i++)
if (tmp_cstr[i]) JS_FreeCString(ctx, tmp_cstr[i]);
return js_ret;
arg_error:
for (int i = 0; i < f->nargs; i++)
if (tmp_cstr[i]) JS_FreeCString(ctx, tmp_cstr[i]);
return JS_EXCEPTION;
}
/* -------------------------------------------------------------------------- */
/* module init */
/* -------------------------------------------------------------------------- */
static const JSCFunctionListEntry js_ffi_funcs[] = {
JS_CFUNC_DEF("dlopen", 1, js_ffi_dlopen),
JS_CFUNC_DEF("prepare", 4, js_ffi_prepare),
};
static const JSCFunctionListEntry js_ffi_func_funcs[] = {
JS_CFUNC_DEF("call", 1, js_ffi_call),
};
JSValue
js_ffi_use(JSContext *ctx)
{
JS_NewClassID(&js_ffi_func_id);
JS_NewClass(JS_GetRuntime(ctx), js_ffi_func_id, &js_ffi_func_class);
JSValue fn_proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, fn_proto, js_ffi_func_funcs, countof(js_ffi_func_funcs));
JS_SetClassProto(ctx, js_ffi_func_id, fn_proto);
JS_NewClassID(&js_ffi_pointer_id);
JS_NewClass(JS_GetRuntime(ctx), js_ffi_pointer_id, &js_ffi_pointer_class);
JSValue mod = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, mod, js_ffi_funcs, countof(js_ffi_funcs));
return mod;
}

8
source/qjs_ffi.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef QJS_FFI_H
#define QJS_FFI_H
#include "cell.h"
JSValue js_ffi_use(JSContext *ctx);
#endif

View File

@@ -884,7 +884,6 @@ static int js_string_compare(JSContext *ctx,
static JSValue JS_ToNumber(JSContext *ctx, JSValueConst val);
static int JS_SetPropertyValue(JSContext *ctx, JSValueConst this_obj,
JSValue prop, JSValue val, int flags);
static int JS_NumberIsInteger(JSContext *ctx, JSValueConst val);
static JSValue JS_ToNumberFree(JSContext *ctx, JSValue val);
static int JS_GetOwnPropertyInternal(JSContext *ctx, JSPropertyDescriptor *desc,
JSObject *p, JSAtom prop);
@@ -2104,9 +2103,6 @@ static JSAtom __JS_NewAtom(JSRuntime *rt, JSString *str, int atom_type)
JSAtomStruct *p;
int len;
#if 0
printf("__JS_NewAtom: "); JS_DumpString(rt, str); printf("\n");
#endif
if (atom_type < JS_ATOM_TYPE_SYMBOL) {
/* str is not NULL */
if (str->atom_type == atom_type) {
@@ -2290,12 +2286,6 @@ static JSAtom __JS_FindAtom(JSRuntime *rt, const char *str, size_t len,
static void JS_FreeAtomStruct(JSRuntime *rt, JSAtomStruct *p)
{
#if 0 /* JS_ATOM_NULL is not refcounted: __JS_AtomIsConst() includes 0 */
if (unlikely(i == JS_ATOM_NULL)) {
p->header.ref_count = INT32_MAX / 2;
return;
}
#endif
uint32_t i = p->hash_next; /* atom_index */
if (p->atom_type != JS_ATOM_TYPE_SYMBOL) {
JSAtomStruct *p0, *p1;
@@ -4537,25 +4527,6 @@ JSValue JS_NewObjectProtoClass(JSContext *ctx, JSValueConst proto_val,
return JS_NewObjectFromShape(ctx, sh, class_id);
}
#if 0
static JSValue JS_GetObjectData(JSContext *ctx, JSValueConst obj)
{
JSObject *p;
if (JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT) {
p = JS_VALUE_GET_OBJ(obj);
switch(p->class_id) {
case JS_CLASS_NUMBER:
case JS_CLASS_STRING:
case JS_CLASS_BOOLEAN:
case JS_CLASS_SYMBOL:
return JS_DupValue(ctx, p->u.object_data);
}
}
return JS_NULL;
}
#endif
static int JS_SetObjectData(JSContext *ctx, JSValueConst obj, JSValue val)
{
JSObject *p;
@@ -7742,9 +7713,7 @@ int JS_SetPropertyInternal(JSContext *ctx, JSValueConst obj,
uint32_t tag;
JSPropertyDescriptor desc;
int ret;
#if 0
printf("JS_SetPropertyInternal: "); print_atom(ctx, prop); printf("\n");
#endif
tag = JS_VALUE_GET_TAG(this_obj);
if (unlikely(tag != JS_TAG_OBJECT)) {
if (JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT) {
@@ -9896,17 +9865,6 @@ static __exception int JS_ToLengthFree(JSContext *ctx, int64_t *plen,
return res;
}
/* Note: can return an exception */
static int JS_NumberIsInteger(JSContext *ctx, JSValueConst val)
{
double d;
if (!JS_IsNumber(val))
return FALSE;
if (unlikely(JS_ToFloat64(ctx, &d, val)))
return -1;
return isfinite(d) && floor(d) == d;
}
static JSValue js_dtoa2(JSContext *ctx,
double d, int radix, int n_digits, int flags)
{
@@ -14723,17 +14681,6 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
}
BREAK;
#if 0
CASE(OP_to_string):
if (JS_VALUE_GET_TAG(sp[-1]) != JS_TAG_STRING) {
ret_val = JS_ToString(ctx, sp[-1]);
if (JS_IsException(ret_val))
goto exception;
JS_FreeValue(ctx, sp[-1]);
sp[-1] = ret_val;
}
BREAK;
#endif
CASE(OP_nop):
BREAK;
CASE(OP_is_null):
@@ -23009,12 +22956,6 @@ static int skip_dead_code(JSFunctionDef *s, const uint8_t *bc_buf, int bc_len,
label = get_u32(bc_buf + pos + 1);
if (update_label(s, label, 0) > 0)
break;
#if 0
if (s->label_slots[label].first_reloc) {
printf("line %d: unreferenced label %d:%d has relocations\n",
*linep, label, s->label_slots[label].pos2);
}
#endif
assert(s->label_slots[label].first_reloc == NULL);
} else {
/* XXX: output a warning for unreachable code? */
@@ -24917,13 +24858,6 @@ static void free_function_bytecode(JSRuntime *rt, JSFunctionBytecode *b)
{
int i;
#if 0
{
char buf[ATOM_GET_STR_BUF_SIZE];
printf("freeing %s\n",
JS_AtomGetStrRT(rt, buf, sizeof(buf), b->func_name));
}
#endif
free_bytecode_atoms(rt, b->byte_code_buf, b->byte_code_len, TRUE);
if (b->vardefs) {
@@ -28478,39 +28412,6 @@ static JSValue js_object_fromEntries(JSContext *ctx, JSValueConst this_val,
return JS_EXCEPTION;
}
#if 0
/* Note: corresponds to ECMA spec: CreateDataPropertyOrThrow() */
static JSValue js_object___setOwnProperty(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int ret;
ret = JS_DefinePropertyValueValue(ctx, argv[0], JS_DupValue(ctx, argv[1]),
JS_DupValue(ctx, argv[2]),
JS_PROP_C_W_E | JS_PROP_THROW);
if (ret < 0)
return JS_EXCEPTION;
else
return JS_NewBool(ctx, ret);
}
static JSValue js_object___toObject(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
return JS_ToObject(ctx, argv[0]);
}
static JSValue js_object___toPrimitive(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int hint = HINT_NONE;
if (JS_VALUE_GET_TAG(argv[1]) == JS_TAG_INT)
hint = JS_VALUE_GET_INT(argv[1]);
return JS_ToPrimitive(ctx, argv[0], hint);
}
#endif
/* return an empty string if not an object */
static JSValue js_object___getClass(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
@@ -28539,46 +28440,6 @@ static JSValue js_object_is(JSContext *ctx, JSValueConst this_val,
return JS_NewBool(ctx, js_same_value(ctx, argv[0], argv[1]));
}
#if 0
static JSValue js_object___getObjectData(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
return JS_GetObjectData(ctx, argv[0]);
}
static JSValue js_object___setObjectData(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
if (JS_SetObjectData(ctx, argv[0], JS_DupValue(ctx, argv[1])))
return JS_EXCEPTION;
return JS_DupValue(ctx, argv[1]);
}
static JSValue js_object___toPropertyKey(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
return JS_ToPropertyKey(ctx, argv[0]);
}
static JSValue js_object___isObject(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
return JS_NewBool(ctx, JS_IsObject(argv[0]));
}
static JSValue js_object___isSameValueZero(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
return JS_NewBool(ctx, js_same_value_zero(ctx, argv[0], argv[1]));
}
static JSValue js_object___isConstructor(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
return JS_NewBool(ctx, JS_IsConstructor(ctx, argv[0]));
}
#endif
static JSValue JS_SpeciesConstructor(JSContext *ctx, JSValueConst obj,
JSValueConst defaultConstructor)
{
@@ -28608,14 +28469,6 @@ static JSValue JS_SpeciesConstructor(JSContext *ctx, JSValueConst obj,
return species;
}
#if 0
static JSValue js_object___speciesConstructor(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
return JS_SpeciesConstructor(ctx, argv[0], argv[1]);
}
#endif
static JSValue js_object_get___proto__(JSContext *ctx, JSValueConst this_val)
{
JSValue val, ret;
@@ -28780,16 +28633,6 @@ static const JSCFunctionListEntry js_object_funcs[] = {
JS_CFUNC_MAGIC_DEF("isSealed", 1, js_object_isSealed, 0 ),
JS_CFUNC_MAGIC_DEF("isFrozen", 1, js_object_isSealed, 1 ),
JS_CFUNC_DEF("__getClass", 1, js_object___getClass ),
//JS_CFUNC_DEF("__isObject", 1, js_object___isObject ),
//JS_CFUNC_DEF("__isConstructor", 1, js_object___isConstructor ),
//JS_CFUNC_DEF("__toObject", 1, js_object___toObject ),
//JS_CFUNC_DEF("__setOwnProperty", 3, js_object___setOwnProperty ),
//JS_CFUNC_DEF("__toPrimitive", 2, js_object___toPrimitive ),
//JS_CFUNC_DEF("__toPropertyKey", 1, js_object___toPropertyKey ),
//JS_CFUNC_DEF("__speciesConstructor", 2, js_object___speciesConstructor ),
//JS_CFUNC_DEF("__isSameValueZero", 2, js_object___isSameValueZero ),
//JS_CFUNC_DEF("__getObjectData", 1, js_object___getObjectData ),
//JS_CFUNC_DEF("__setObjectData", 2, js_object___setObjectData ),
JS_CFUNC_DEF("fromEntries", 1, js_object_fromEntries ),
JS_CFUNC_DEF("hasOwn", 2, js_object_hasOwn ),
};
@@ -31291,23 +31134,6 @@ static JSValue js_number_constructor(JSContext *ctx, JSValueConst new_target,
}
}
#if 0
static JSValue js_number___toInteger(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
return JS_ToIntegerFree(ctx, JS_DupValue(ctx, argv[0]));
}
static JSValue js_number___toLength(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int64_t v;
if (JS_ToLengthFree(ctx, &v, JS_DupValue(ctx, argv[0])))
return JS_EXCEPTION;
return JS_NewInt64(ctx, v);
}
#endif
static JSValue js_number_isNaN(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
@@ -31324,46 +31150,13 @@ static JSValue js_number_isFinite(JSContext *ctx, JSValueConst this_val,
return js_global_isFinite(ctx, this_val, argc, argv);
}
static JSValue js_number_isInteger(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int ret;
ret = JS_NumberIsInteger(ctx, argv[0]);
if (ret < 0)
return JS_EXCEPTION;
else
return JS_NewBool(ctx, ret);
}
static JSValue js_number_isSafeInteger(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
double d;
if (!JS_IsNumber(argv[0]))
return JS_FALSE;
if (unlikely(JS_ToFloat64(ctx, &d, argv[0])))
return JS_EXCEPTION;
return JS_NewBool(ctx, is_safe_integer(d));
}
static const JSCFunctionListEntry js_number_funcs[] = {
/* global ParseInt and parseFloat should be defined already or delayed */
JS_ALIAS_BASE_DEF("parseInt", "parseInt", 0 ),
JS_ALIAS_BASE_DEF("parseFloat", "parseFloat", 0 ),
JS_CFUNC_DEF("isNaN", 1, js_number_isNaN ),
JS_CFUNC_DEF("isFinite", 1, js_number_isFinite ),
JS_CFUNC_DEF("isInteger", 1, js_number_isInteger ),
JS_CFUNC_DEF("isSafeInteger", 1, js_number_isSafeInteger ),
JS_PROP_DOUBLE_DEF("MAX_VALUE", 1.7976931348623157e+308, 0 ),
JS_PROP_DOUBLE_DEF("MIN_VALUE", 5e-324, 0 ),
JS_PROP_DOUBLE_DEF("NaN", NAN, 0 ),
JS_PROP_DOUBLE_DEF("NEGATIVE_INFINITY", -INFINITY, 0 ),
JS_PROP_DOUBLE_DEF("POSITIVE_INFINITY", INFINITY, 0 ),
JS_PROP_DOUBLE_DEF("EPSILON", 2.220446049250313e-16, 0 ), /* ES6 */
JS_PROP_DOUBLE_DEF("MAX_SAFE_INTEGER", 9007199254740991.0, 0 ), /* ES6 */
JS_PROP_DOUBLE_DEF("MIN_SAFE_INTEGER", -9007199254740991.0, 0 ), /* ES6 */
//JS_CFUNC_DEF("__toInteger", 1, js_number___toInteger ),
//JS_CFUNC_DEF("__toLength", 1, js_number___toLength ),
};
static JSValue js_thisNumberValue(JSContext *ctx, JSValueConst this_val)
@@ -31871,17 +31664,6 @@ JSValue js_string_codePointRange(JSContext *ctx, JSValueConst this_val,
return string_buffer_end(b);
}
#if 0
static JSValue js_string___isSpace(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int c;
if (JS_ToInt32(ctx, &c, argv[0]))
return JS_EXCEPTION;
return JS_NewBool(ctx, lre_is_space(c));
}
#endif
static JSValue js_string_charCodeAt(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
@@ -33165,47 +32947,6 @@ static JSValue js_string_toString(JSContext *ctx, JSValueConst this_val,
return js_thisStringValue(ctx, this_val);
}
#if 0
static JSValue js_string___toStringCheckObject(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
return JS_ToStringCheckObject(ctx, argv[0]);
}
static JSValue js_string___toString(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
return JS_ToString(ctx, argv[0]);
}
static JSValue js_string___advanceStringIndex(JSContext *ctx, JSValueConst
this_val,
int argc, JSValueConst *argv)
{
JSValue str;
int idx;
BOOL is_unicode;
JSString *p;
str = JS_ToString(ctx, argv[0]);
if (JS_IsException(str))
return str;
if (JS_ToInt32Sat(ctx, &idx, argv[1])) {
JS_FreeValue(ctx, str);
return JS_EXCEPTION;
}
is_unicode = JS_ToBool(ctx, argv[2]);
p = JS_VALUE_GET_STRING(str);
if (!is_unicode || (unsigned)idx >= p->len || !p->is_wide_char) {
idx++;
} else {
string_getc(p, &idx);
}
JS_FreeValue(ctx, str);
return JS_NewInt32(ctx, idx);
}
#endif
/* String Iterator */
static JSValue js_string_iterator_next(JSContext *ctx, JSValueConst this_val,
@@ -33265,11 +33006,6 @@ static const JSCFunctionListEntry js_string_funcs[] = {
JS_CFUNC_DEF("fromCharCode", 1, js_string_fromCharCode ),
JS_CFUNC_DEF("fromCodePoint", 1, js_string_fromCodePoint ),
JS_CFUNC_DEF("raw", 1, js_string_raw ),
//JS_CFUNC_DEF("__toString", 1, js_string___toString ),
//JS_CFUNC_DEF("__isSpace", 1, js_string___isSpace ),
//JS_CFUNC_DEF("__toStringCheckObject", 1, js_string___toStringCheckObject ),
//JS_CFUNC_DEF("__advanceStringIndex", 3, js_string___advanceStringIndex ),
//JS_CFUNC_DEF("__GetSubstitution", 6, js_string___GetSubstitution ),
};
static const JSCFunctionListEntry js_string_proto_funcs[] = {
@@ -33308,8 +33044,6 @@ static const JSCFunctionListEntry js_string_proto_funcs[] = {
JS_CFUNC_DEF("__quote", 1, js_string___quote ),
JS_CFUNC_MAGIC_DEF("toLowerCase", 0, js_string_toLowerCase, 1 ),
JS_CFUNC_MAGIC_DEF("toUpperCase", 0, js_string_toLowerCase, 0 ),
// JS_CFUNC_MAGIC_DEF("toLocaleLowerCase", 0, js_string_toLowerCase, 1 ),
// JS_CFUNC_MAGIC_DEF("toLocaleUpperCase", 0, js_string_toLowerCase, 0 ),
JS_CFUNC_MAGIC_DEF("[Symbol.iterator]", 0, js_create_array_iterator, JS_ITERATOR_KIND_VALUE | 4 ),
};
@@ -33476,45 +33210,6 @@ static JSValue js_math_hypot(JSContext *ctx, JSValueConst this_val,
return JS_NewFloat64(ctx, r);
}
static double js_math_f16round(double a)
{
return fromfp16(tofp16(a));
}
static double js_math_fround(double a)
{
return (float)a;
}
static JSValue js_math_imul(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
uint32_t a, b, c;
int32_t d;
if (JS_ToUint32(ctx, &a, argv[0]))
return JS_EXCEPTION;
if (JS_ToUint32(ctx, &b, argv[1]))
return JS_EXCEPTION;
c = a * b;
memcpy(&d, &c, sizeof(d));
return JS_NewInt32(ctx, d);
}
static JSValue js_math_clz32(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
uint32_t a, r;
if (JS_ToUint32(ctx, &a, argv[0]))
return JS_EXCEPTION;
if (a == 0)
r = 32;
else
r = clz32(a);
return JS_NewInt32(ctx, r);
}
/* xorshift* random number generator by Marsaglia */
static uint64_t xorshift64star(uint64_t *pstate)
{
@@ -33581,46 +33276,17 @@ static const JSCFunctionListEntry js_math_funcs[] = {
JS_CFUNC_SPECIAL_DEF("log1p", 1, f_f, log1p ),
JS_CFUNC_SPECIAL_DEF("log2", 1, f_f, log2 ),
JS_CFUNC_SPECIAL_DEF("log10", 1, f_f, log10 ),
JS_CFUNC_SPECIAL_DEF("cbrt", 1, f_f, cbrt ),
JS_CFUNC_DEF("hypot", 2, js_math_hypot ),
JS_CFUNC_DEF("random", 0, js_math_random ),
JS_CFUNC_SPECIAL_DEF("f16round", 1, f_f, js_math_f16round ),
JS_CFUNC_SPECIAL_DEF("fround", 1, f_f, js_math_fround ),
JS_CFUNC_DEF("imul", 2, js_math_imul ),
JS_CFUNC_DEF("clz32", 1, js_math_clz32 ),
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Math", JS_PROP_CONFIGURABLE ),
JS_PROP_DOUBLE_DEF("E", 2.718281828459045, 0 ),
JS_PROP_DOUBLE_DEF("LN10", 2.302585092994046, 0 ),
JS_PROP_DOUBLE_DEF("LN2", 0.6931471805599453, 0 ),
JS_PROP_DOUBLE_DEF("LOG2E", 1.4426950408889634, 0 ),
JS_PROP_DOUBLE_DEF("LOG10E", 0.4342944819032518, 0 ),
JS_PROP_DOUBLE_DEF("PI", 3.141592653589793, 0 ),
JS_PROP_DOUBLE_DEF("SQRT1_2", 0.7071067811865476, 0 ),
JS_PROP_DOUBLE_DEF("SQRT2", 1.4142135623730951, 0 ),
};
static const JSCFunctionListEntry js_math_obj[] = {
JS_OBJECT_DEF("Math", js_math_funcs, countof(js_math_funcs), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE ),
};
#if 0
static JSValue js_get_prototype_from_ctor(JSContext *ctx, JSValueConst ctor,
JSValueConst def_proto)
{
JSValue proto;
proto = JS_GetProperty(ctx, ctor, JS_ATOM_prototype);
if (JS_IsException(proto))
return proto;
if (!JS_IsObject(proto)) {
JS_FreeValue(ctx, proto);
proto = JS_DupValue(ctx, def_proto);
}
return proto;
}
#endif
/* RegExp */
static void js_regexp_finalizer(JSRuntime *rt, JSValue val)
@@ -33891,27 +33557,6 @@ static JSValue js_regexp_compile(JSContext *ctx, JSValueConst this_val,
return JS_EXCEPTION;
}
#if 0
static JSValue js_regexp_get___source(JSContext *ctx, JSValueConst this_val)
{
JSRegExp *re = js_get_regexp(ctx, this_val, TRUE);
if (!re)
return JS_EXCEPTION;
return JS_DupValue(ctx, JS_MKPTR(JS_TAG_STRING, re->pattern));
}
static JSValue js_regexp_get___flags(JSContext *ctx, JSValueConst this_val)
{
JSRegExp *re = js_get_regexp(ctx, this_val, TRUE);
int flags;
if (!re)
return JS_EXCEPTION;
flags = lre_get_flags(re->bytecode->u.str8);
return JS_NewInt32(ctx, flags);
}
#endif
static JSValue js_regexp_get_source(JSContext *ctx, JSValueConst this_val)
{
JSRegExp *re;
@@ -34455,19 +34100,6 @@ static JSValue JS_RegExpExec(JSContext *ctx, JSValueConst r, JSValueConst s)
return js_regexp_exec(ctx, r, 1, &s);
}
#if 0
static JSValue js_regexp___RegExpExec(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
return JS_RegExpExec(ctx, argv[0], argv[1]);
}
static JSValue js_regexp___RegExpDelete(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
return JS_RegExpDelete(ctx, argv[0], argv[1]);
}
#endif
static JSValue js_regexp_test(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
@@ -35177,8 +34809,6 @@ done:
static const JSCFunctionListEntry js_regexp_funcs[] = {
JS_CFUNC_DEF("escape", 1, js_regexp_escape ),
JS_CGETSET_DEF("[Symbol.species]", js_get_this, NULL ),
//JS_CFUNC_DEF("__RegExpExec", 2, js_regexp___RegExpExec ),
//JS_CFUNC_DEF("__RegExpDelete", 2, js_regexp___RegExpDelete ),
};
static const JSCFunctionListEntry js_regexp_proto_funcs[] = {
@@ -35201,8 +34831,6 @@ static const JSCFunctionListEntry js_regexp_proto_funcs[] = {
JS_CFUNC_DEF("[Symbol.matchAll]", 1, js_regexp_Symbol_matchAll ),
JS_CFUNC_DEF("[Symbol.search]", 1, js_regexp_Symbol_search ),
JS_CFUNC_DEF("[Symbol.split]", 2, js_regexp_Symbol_split ),
//JS_CGETSET_DEF("__source", js_regexp_get___source, NULL ),
//JS_CGETSET_DEF("__flags", js_regexp_get___flags, NULL ),
};
static const JSCFunctionListEntry js_regexp_string_iterator_proto_funcs[] = {
@@ -38144,16 +37772,7 @@ static void JS_AddIntrinsicBasicObjects(JSContext *ctx)
ctx->class_proto[JS_CLASS_OBJECT]);
ctx->class_proto[JS_CLASS_BYTECODE_FUNCTION] = JS_DupValue(ctx, ctx->function_proto);
ctx->class_proto[JS_CLASS_ERROR] = JS_NewObject(ctx);
#if 0
/* these are auto-initialized from js_error_proto_funcs,
but delaying might be a problem */
JS_DefinePropertyValue(ctx, ctx->class_proto[JS_CLASS_ERROR], JS_ATOM_name,
JS_AtomToString(ctx, JS_ATOM_Error),
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
JS_DefinePropertyValue(ctx, ctx->class_proto[JS_CLASS_ERROR], JS_ATOM_message,
JS_AtomToString(ctx, JS_ATOM_empty_string),
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
#endif
JS_SetPropertyFunctionList(ctx, ctx->class_proto[JS_CLASS_ERROR],
js_error_proto_funcs,
countof(js_error_proto_funcs));