some cleanup
This commit is contained in:
@@ -1,27 +1,15 @@
|
||||
#include "cell.h"
|
||||
|
||||
// Return the current stack depth.
|
||||
JSC_CCALL(debug_stack_depth, return number2js(js,js_debugger_stack_depth(js)))
|
||||
// TODO: Reimplement stack depth for register VM
|
||||
JSC_CCALL(debug_stack_depth, return number2js(js, 0))
|
||||
|
||||
// Return a backtrace of the current call stack.
|
||||
JSC_CCALL(debug_build_backtrace, return js_debugger_build_backtrace(js))
|
||||
|
||||
// Return the closure variables for a given function.
|
||||
JSC_CCALL(debug_closure_vars, return js_debugger_closure_variables(js,argv[0]))
|
||||
|
||||
JSC_CCALL(debug_set_closure_var,
|
||||
js_debugger_set_closure_variable(js,argv[0],argv[1],argv[2]);
|
||||
return JS_NULL;
|
||||
)
|
||||
|
||||
// Return the local variables for a specific stack frame.
|
||||
JSC_CCALL(debug_local_vars, return js_debugger_local_variables(js, js2number(js,argv[0])))
|
||||
|
||||
// Return metadata about a given function.
|
||||
JSC_CCALL(debug_fn_info, return js_debugger_fn_info(js, argv[0]))
|
||||
|
||||
// Return an array of functions in the current backtrace.
|
||||
JSC_CCALL(debug_backtrace_fns, return js_debugger_backtrace_fns(js))
|
||||
// TODO: Reimplement debug introspection for register VM
|
||||
JSC_CCALL(debug_build_backtrace, return JS_NewArray(js))
|
||||
JSC_CCALL(debug_closure_vars, return JS_NewObject(js))
|
||||
JSC_CCALL(debug_set_closure_var, return JS_NULL;)
|
||||
JSC_CCALL(debug_local_vars, return JS_NewObject(js))
|
||||
JSC_CCALL(debug_fn_info, return JS_NewObject(js))
|
||||
JSC_CCALL(debug_backtrace_fns, return JS_NewArray(js))
|
||||
|
||||
static const JSCFunctionListEntry js_debug_funcs[] = {
|
||||
MIST_FUNC_DEF(debug, stack_depth, 0),
|
||||
@@ -37,4 +25,4 @@ JSValue js_debug_use(JSContext *js) {
|
||||
JSValue mod = JS_NewObject(js);
|
||||
JS_SetPropertyFunctionList(js,mod,js_debug_funcs,countof(js_debug_funcs));
|
||||
return mod;
|
||||
}
|
||||
}
|
||||
|
||||
42
debug/js.c
42
debug/js.c
@@ -1,48 +1,16 @@
|
||||
#include "cell.h"
|
||||
|
||||
JSC_CCALL(os_mem_limit, JS_SetMemoryLimit(JS_GetRuntime(js), js2number(js,argv[0])))
|
||||
JSC_CCALL(os_max_stacksize, JS_SetMaxStackSize(JS_GetRuntime(js), js2number(js,argv[0])))
|
||||
JSC_CCALL(os_max_stacksize, JS_SetMaxStackSize(js, js2number(js,argv[0])))
|
||||
|
||||
// Compute the approximate size of a single JS value in memory.
|
||||
// TODO: Reimplement memory usage reporting for new allocator
|
||||
JSC_CCALL(os_calc_mem,
|
||||
JSMemoryUsage mu;
|
||||
JS_ComputeMemoryUsage(JS_GetRuntime(js),&mu);
|
||||
ret = JS_NewObject(js);
|
||||
JS_SetPropertyStr(js,ret,"malloc_size",number2js(js,mu.malloc_size));
|
||||
JS_SetPropertyStr(js,ret,"malloc_limit",number2js(js,mu.malloc_limit));
|
||||
JS_SetPropertyStr(js,ret,"memory_used_size",number2js(js,mu.memory_used_size));
|
||||
JS_SetPropertyStr(js,ret,"malloc_count",number2js(js,mu.malloc_count));
|
||||
JS_SetPropertyStr(js,ret,"memory_used_count",number2js(js,mu.memory_used_count));
|
||||
JS_SetPropertyStr(js,ret,"str_count",number2js(js,mu.str_count));
|
||||
JS_SetPropertyStr(js,ret,"str_size",number2js(js,mu.str_size));
|
||||
JS_SetPropertyStr(js,ret,"obj_count",number2js(js,mu.obj_count));
|
||||
JS_SetPropertyStr(js,ret,"obj_size",number2js(js,mu.obj_size));
|
||||
JS_SetPropertyStr(js,ret,"prop_count",number2js(js,mu.prop_count));
|
||||
JS_SetPropertyStr(js,ret,"prop_size",number2js(js,mu.prop_size));
|
||||
JS_SetPropertyStr(js,ret,"shape_count",number2js(js,mu.shape_count));
|
||||
JS_SetPropertyStr(js,ret,"shape_size",number2js(js,mu.shape_size));
|
||||
JS_SetPropertyStr(js,ret,"js_func_count",number2js(js,mu.js_func_count));
|
||||
JS_SetPropertyStr(js,ret,"js_func_size",number2js(js,mu.js_func_size));
|
||||
JS_SetPropertyStr(js,ret,"js_func_code_size",number2js(js,mu.js_func_code_size));
|
||||
JS_SetPropertyStr(js,ret,"js_func_pc2line_count",number2js(js,mu.js_func_pc2line_count));
|
||||
JS_SetPropertyStr(js,ret,"js_func_pc2line_size",number2js(js,mu.js_func_pc2line_size));
|
||||
JS_SetPropertyStr(js,ret,"c_func_count",number2js(js,mu.c_func_count));
|
||||
JS_SetPropertyStr(js,ret,"array_count",number2js(js,mu.array_count));
|
||||
JS_SetPropertyStr(js,ret,"fast_array_count",number2js(js,mu.fast_array_count));
|
||||
JS_SetPropertyStr(js,ret,"fast_array_elements",number2js(js,mu.fast_array_elements));
|
||||
JS_SetPropertyStr(js,ret,"binary_object_count",number2js(js,mu.binary_object_count));
|
||||
JS_SetPropertyStr(js,ret,"binary_object_size",number2js(js,mu.binary_object_size));
|
||||
)
|
||||
|
||||
// Disassemble a function object into a string.
|
||||
JSC_CCALL(js_disassemble,
|
||||
return js_debugger_fn_bytecode(js, argv[0]);
|
||||
)
|
||||
|
||||
// Return metadata about a given function.
|
||||
JSC_CCALL(js_fn_info,
|
||||
return js_debugger_fn_info(js, argv[0]);
|
||||
)
|
||||
// TODO: Reimplement for register VM
|
||||
JSC_CCALL(js_disassemble, return JS_NewArray(js);)
|
||||
JSC_CCALL(js_fn_info, return JS_NewObject(js);)
|
||||
|
||||
static const JSCFunctionListEntry js_js_funcs[] = {
|
||||
MIST_FUNC_DEF(os, calc_mem, 0),
|
||||
|
||||
@@ -78,8 +78,7 @@ void cell_trace_sethook(cell_hook);
|
||||
#define QJSCLASS(TYPE, ...)\
|
||||
JSClassID js_##TYPE##_id;\
|
||||
static void js_##TYPE##_finalizer(JSRuntime *rt, JSValue val){\
|
||||
JSContext *js = JS_GetContext(rt);\
|
||||
TYPE *n = JS_GetOpaque2(js, val, js_##TYPE##_id); \
|
||||
TYPE *n = JS_GetOpaque(val, js_##TYPE##_id); \
|
||||
TYPE##_free(rt,n);}\
|
||||
static JSClassDef js_##TYPE##_class = {\
|
||||
.class_name = #TYPE,\
|
||||
|
||||
@@ -307,9 +307,6 @@ typedef enum JSErrorEnum {
|
||||
/* Compatibility: JS_TAG_STRING is an alias for text type checks */
|
||||
#define JS_TAG_STRING JS_TAG_STRING_IMM
|
||||
|
||||
/* JS_TAG_FUNCTION doesn't exist in new encoding - use JS_IsFunction check instead */
|
||||
#define JS_TAG_FUNCTION 0xFE /* dummy value, never matches any tag */
|
||||
|
||||
/* JS_ThrowMemoryError is an alias for JS_ThrowOutOfMemory */
|
||||
#define JS_ThrowMemoryError(ctx) JS_ThrowOutOfMemory(ctx)
|
||||
|
||||
@@ -318,8 +315,6 @@ static inline objhdr_t objhdr_set_cap56 (objhdr_t h, uint64_t cap) {
|
||||
return (h & 0xFF) | ((cap & OBJHDR_CAP_MASK) << OBJHDR_CAP_SHIFT);
|
||||
}
|
||||
|
||||
typedef enum OPCodeEnum OPCodeEnum;
|
||||
|
||||
/* ============================================================
|
||||
Buddy Allocator for Actor Memory Blocks
|
||||
============================================================ */
|
||||
@@ -775,27 +770,6 @@ typedef struct JSFrame {
|
||||
JSValue slots[]; /* [this][args][captured][locals][temps] */
|
||||
} JSFrame;
|
||||
|
||||
/* Execution state returned by vm_execute_frame */
|
||||
typedef enum {
|
||||
VM_EXEC_NORMAL, /* Continue executing current frame */
|
||||
VM_EXEC_RETURN, /* Frame returned, pop and resume caller */
|
||||
VM_EXEC_CALL, /* Need to push new frame for call */
|
||||
VM_EXEC_EXCEPTION, /* Exception thrown, unwind frames */
|
||||
} VMExecState;
|
||||
|
||||
/* Call info for frame push */
|
||||
typedef struct {
|
||||
JSValue func_obj;
|
||||
JSValue this_obj;
|
||||
int argc;
|
||||
JSValue *argv;
|
||||
const uint8_t *ret_pc;
|
||||
int ret_sp_offset;
|
||||
int call_argc;
|
||||
int call_has_this;
|
||||
int is_tail_call;
|
||||
} VMCallInfo;
|
||||
|
||||
static inline objhdr_t objhdr_set_s (objhdr_t h, bool s) {
|
||||
return s ? (h | OBJHDR_S_MASK) : (h & ~OBJHDR_S_MASK);
|
||||
}
|
||||
@@ -1067,9 +1041,6 @@ struct JSContext {
|
||||
// todo: want this, but should be a simple increment/decrement counter while frames are pushed
|
||||
size_t stack_depth;
|
||||
size_t stack_limit;
|
||||
|
||||
/* Parser state (for GC to scan cpool during parsing) */
|
||||
struct JSFunctionDef *current_parse_fd;
|
||||
};
|
||||
|
||||
/* ============================================================
|
||||
@@ -1244,46 +1215,6 @@ typedef struct JSFunction {
|
||||
} u;
|
||||
} JSFunction;
|
||||
|
||||
/* for the encoding of the pc2line table */
|
||||
#define PC2LINE_BASE (-1)
|
||||
#define PC2LINE_RANGE 5
|
||||
#define PC2LINE_OP_FIRST 1
|
||||
#define PC2LINE_DIFF_PC_MAX ((255 - PC2LINE_OP_FIRST) / PC2LINE_RANGE)
|
||||
|
||||
/* New simplified compiled unit structure for Phase 1+ simplification.
|
||||
Replaces JSFunctionBytecode with a simpler model:
|
||||
- No closure machinery (uses outer_frame chain at runtime)
|
||||
- Free variables resolved at link time against env + globals
|
||||
- Nested functions stored as separate units in cpool */
|
||||
typedef struct JSCompiledUnit {
|
||||
objhdr_t header; /* must come first */
|
||||
|
||||
/* Bytecode (self pointer) */
|
||||
uint8_t *byte_code_buf;
|
||||
int byte_code_len;
|
||||
|
||||
/* Constants - strings, numbers, nested unit refs (self pointer) */
|
||||
JSValue *cpool;
|
||||
int cpool_count;
|
||||
|
||||
/* Stack requirements */
|
||||
uint16_t local_count; /* total local slots (args + vars) */
|
||||
uint16_t stack_size; /* operand stack depth */
|
||||
|
||||
/* Flags */
|
||||
uint8_t has_debug : 1;
|
||||
uint8_t read_only_bytecode : 1;
|
||||
|
||||
/* Debug info (optional - only present if has_debug) */
|
||||
struct {
|
||||
JSValue filename;
|
||||
int source_len;
|
||||
int pc2line_len;
|
||||
uint8_t *pc2line_buf;
|
||||
char *source;
|
||||
} debug;
|
||||
} JSCompiledUnit;
|
||||
|
||||
/* ============================================================
|
||||
Context-Neutral Module Format (Phase 2+)
|
||||
Struct definitions are in quickjs.h
|
||||
@@ -1301,7 +1232,6 @@ typedef struct JSCompiledUnit {
|
||||
#endif
|
||||
|
||||
JSValue js_call_c_function (JSContext *ctx, JSValue func_obj, JSValue this_obj, int argc, JSValue *argv);
|
||||
JSValue js_call_bound_function (JSContext *ctx, JSValue func_obj, JSValue this_obj, int argc, JSValue *argv);
|
||||
JSValue JS_CallInternal (JSContext *ctx, JSValue func_obj, JSValue this_obj, int argc, JSValue *argv, int flags);
|
||||
JSValue JS_CallRegisterVM(JSContext *ctx, JSCodeRegister *code, JSValue this_obj, int argc, JSValue *argv, JSValue env, JSValue outer_frame);
|
||||
int JS_DeleteProperty (JSContext *ctx, JSValue obj, JSValue prop);
|
||||
@@ -1319,9 +1249,6 @@ JSValue js_new_function (JSContext *ctx, JSFunctionKind kind);
|
||||
/* Forward declaration - helper to set cap in objhdr */
|
||||
static inline objhdr_t objhdr_set_cap56 (objhdr_t h, uint64_t cap);
|
||||
|
||||
/* JS_ThrowMemoryError is an alias for JS_ThrowOutOfMemory */
|
||||
#define JS_ThrowMemoryError(ctx) JS_ThrowOutOfMemory(ctx)
|
||||
|
||||
/* GC-SAFE: JS_SetPropertyInternal: same as JS_SetProperty but doesn't check stone.
|
||||
Internal use only. May trigger GC if record needs to resize. */
|
||||
|
||||
@@ -1508,8 +1435,6 @@ typedef struct PPretext {
|
||||
int cap;
|
||||
} PPretext;
|
||||
|
||||
#define JS_CALL_FLAG_COPY_ARGV (1 << 1)
|
||||
|
||||
extern JSClassID js_class_id_alloc;
|
||||
|
||||
/* === Forward declarations for functions split across modules === */
|
||||
@@ -1519,12 +1444,6 @@ int ctx_gc (JSContext *ctx, int allow_grow, size_t alloc_size);
|
||||
JSValue JS_CallInternal (JSContext *ctx, JSValue func_obj, JSValue this_obj, int argc, JSValue *argv, int flags);
|
||||
|
||||
/* runtime.c exports */
|
||||
JSValue JS_ThrowStackOverflow (JSContext *ctx);
|
||||
int JS_DefineObjectName (JSContext *ctx, JSValue obj, JSValue name);
|
||||
int JS_DefineObjectNameComputed (JSContext *ctx, JSValue obj, JSValue str);
|
||||
int js_method_set_properties (JSContext *ctx, JSValue func_obj, JSValue name, int flags, JSValue home_obj);
|
||||
JSValue JS_GetPropertyValue (JSContext *ctx, JSValue this_obj, JSValue prop);
|
||||
__exception int JS_CopyDataProperties (JSContext *ctx, JSValue target, JSValue source, JSValue excluded, BOOL setprop);
|
||||
int js_string_compare_value (JSContext *ctx, JSValue op1, JSValue op2, BOOL eq_only);
|
||||
int js_string_compare_value_nocase (JSContext *ctx, JSValue op1, JSValue op2);
|
||||
JSValue js_regexp_constructor (JSContext *ctx, JSValue this_val, int argc, JSValue *argv);
|
||||
@@ -1533,11 +1452,6 @@ int JS_HasPropertyKey (JSContext *ctx, JSValue obj, JSValue key);
|
||||
void *js_realloc_rt (void *ptr, size_t size);
|
||||
char *js_strdup_rt (const char *str);
|
||||
JSValue JS_ConcatString (JSContext *ctx, JSValue op1, JSValue op2);
|
||||
__exception int js_post_inc_slow (JSContext *ctx, JSValue *sp, OPCodeEnum op);
|
||||
no_inline int js_not_slow (JSContext *ctx, JSValue *sp);
|
||||
no_inline int js_relational_slow (JSContext *ctx, JSValue *sp, OPCodeEnum op);
|
||||
__exception int js_operator_in (JSContext *ctx, JSValue *sp);
|
||||
__exception int js_operator_delete (JSContext *ctx, JSValue *sp);
|
||||
JSText *pretext_init (JSContext *ctx, int capacity);
|
||||
JSText *pretext_putc (JSContext *ctx, JSText *s, uint32_t c);
|
||||
JSText *pretext_concat_value (JSContext *ctx, JSText *s, JSValue v);
|
||||
@@ -1563,19 +1477,6 @@ uint64_t get_text_hash (JSText *text);
|
||||
void pack_utf32_to_words (const uint32_t *utf32, uint32_t len, uint64_t *packed);
|
||||
int text_equal (JSText *a, const uint64_t *packed_b, uint32_t len_b);
|
||||
|
||||
static inline JSValue *get_upvalue_ptr (JSValue frame_val, int depth, int slot) {
|
||||
if (JS_IsNull(frame_val)) return NULL;
|
||||
JSFrame *frame = JS_VALUE_GET_FRAME(frame_val);
|
||||
while (depth > 0) {
|
||||
JSFunction *fn = JS_VALUE_GET_FUNCTION(frame->function);
|
||||
frame_val = fn->u.reg.outer_frame;
|
||||
if (JS_IsNull(frame_val)) return NULL;
|
||||
frame = JS_VALUE_GET_FRAME(frame_val);
|
||||
depth--;
|
||||
}
|
||||
return &frame->slots[slot];
|
||||
}
|
||||
|
||||
void print_backtrace (JSContext *ctx, const char *filename, int line_num, int col_num);
|
||||
JSValue JS_ThrowError2 (JSContext *ctx, JSErrorEnum error_num, const char *fmt, va_list ap, BOOL add_backtrace);
|
||||
JSValue gc_copy_value (JSContext *ctx, JSValue v, uint8_t *from_base, uint8_t *from_end, uint8_t *to_base, uint8_t **to_free, uint8_t *to_end);
|
||||
@@ -1587,10 +1488,6 @@ PPretext *ppretext_append_jsvalue (PPretext *p, JSValue str);
|
||||
PPretext *ppretext_append_int (PPretext *p, int n);
|
||||
JSValue js_atof (JSContext *ctx, const char *str, const char **pp, int radix, int flags);
|
||||
|
||||
/* Defines from runtime section needed by cell_js.c */
|
||||
#define DEFINE_GLOBAL_LEX_VAR (1 << 7)
|
||||
#define DEFINE_GLOBAL_FUNC_VAR (1 << 6)
|
||||
|
||||
#define ATOD_INT_ONLY (1 << 0)
|
||||
/* accept Oo and Ob prefixes in addition to 0x prefix if radix = 0 */
|
||||
#define ATOD_ACCEPT_BIN_OCT (1 << 2)
|
||||
@@ -1607,14 +1504,6 @@ JSValue js_atof (JSContext *ctx, const char *str, const char **pp, int radix, in
|
||||
/* accept -0x1 */
|
||||
#define ATOD_ACCEPT_PREFIX_AFTER_SIGN (1 << 10)
|
||||
|
||||
#define GLOBAL_VAR_OFFSET 0x40000000
|
||||
#define ARGUMENT_VAR_OFFSET 0x20000000
|
||||
|
||||
/* Inline functions from runtime section needed by cell_js.c */
|
||||
static inline void js_dbuf_init (JSContext *ctx, DynBuf *s) {
|
||||
dbuf_init2 (s, ctx->rt, NULL);
|
||||
}
|
||||
|
||||
static inline int to_digit (int c) {
|
||||
if (c >= '0' && c <= '9') return c - '0';
|
||||
else if (c >= 'A' && c <= 'Z') return c - 'A' + 10;
|
||||
@@ -1622,54 +1511,6 @@ static inline int to_digit (int c) {
|
||||
else return 36;
|
||||
}
|
||||
|
||||
static inline void dbuf_put_leb128 (DynBuf *s, uint32_t v) {
|
||||
uint32_t a;
|
||||
for (;;) {
|
||||
a = v & 0x7f;
|
||||
v >>= 7;
|
||||
if (v != 0) {
|
||||
dbuf_putc (s, a | 0x80);
|
||||
} else {
|
||||
dbuf_putc (s, a);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline void dbuf_put_sleb128 (DynBuf *s, int32_t v1) {
|
||||
uint32_t v = v1;
|
||||
dbuf_put_leb128 (s, (2 * v) ^ -(v >> 31));
|
||||
}
|
||||
|
||||
static inline int get_leb128 (uint32_t *pval, const uint8_t *buf, const uint8_t *buf_end) {
|
||||
const uint8_t *ptr = buf;
|
||||
uint32_t v, a, i;
|
||||
v = 0;
|
||||
for (i = 0; i < 5; i++) {
|
||||
if (unlikely (ptr >= buf_end)) break;
|
||||
a = *ptr++;
|
||||
v |= (a & 0x7f) << (i * 7);
|
||||
if (!(a & 0x80)) {
|
||||
*pval = v;
|
||||
return ptr - buf;
|
||||
}
|
||||
}
|
||||
*pval = 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static inline int get_sleb128 (int32_t *pval, const uint8_t *buf, const uint8_t *buf_end) {
|
||||
int ret;
|
||||
uint32_t val;
|
||||
ret = get_leb128 (&val, buf, buf_end);
|
||||
if (ret < 0) {
|
||||
*pval = 0;
|
||||
return -1;
|
||||
}
|
||||
*pval = (val >> 1) ^ -(val & 1);
|
||||
return ret;
|
||||
}
|
||||
|
||||
no_inline int js_realloc_array (JSContext *ctx, void **parray, int elem_size, int *psize, int req_size);
|
||||
static inline int js_resize_array (JSContext *ctx, void **parray, int elem_size, int *psize, int req_size) {
|
||||
if (unlikely (req_size > *psize))
|
||||
@@ -1684,7 +1525,6 @@ JSValue js_key_from_string (JSContext *ctx, JSValue val);
|
||||
/* mach.c exports */
|
||||
JSValue JS_CallRegisterVM(JSContext *ctx, JSCodeRegister *code, JSValue this_obj, int argc, JSValue *argv, JSValue env, JSValue outer_frame);
|
||||
JSFrameRegister *alloc_frame_register(JSContext *ctx, int slot_count);
|
||||
cJSON *mach_find_scope_record(cJSON *scopes, int function_nr);
|
||||
int reg_vm_check_interrupt(JSContext *ctx);
|
||||
|
||||
|
||||
|
||||
@@ -283,13 +283,6 @@ JS_IsShortFloat (JSValue v) {
|
||||
#define JS_TRUE ((JSValue)(JS_TAG_BOOL | (1 << 5)))
|
||||
#define JS_EXCEPTION ((JSValue)JS_TAG_EXCEPTION)
|
||||
|
||||
/* flags for object properties - simplified model:
|
||||
- No per-property writable/configurable (use stone() for immutability)
|
||||
- Text keys are enumerable, object keys are not */
|
||||
#define JS_PROP_TMASK (3 << 4) /* mask for NORMAL, VARREF */
|
||||
#define JS_PROP_NORMAL (0 << 4)
|
||||
#define JS_PROP_VARREF (2 << 4) /* used internally for closures */
|
||||
|
||||
#ifndef JS_DEFAULT_STACK_SIZE
|
||||
#define JS_DEFAULT_STACK_SIZE (1024 * 1024)
|
||||
#endif
|
||||
@@ -312,16 +305,10 @@ JSRuntime *JS_NewRuntime (void);
|
||||
void JS_SetMemoryLimit (JSRuntime *rt, size_t limit);
|
||||
/* use 0 to disable maximum stack size check */
|
||||
void JS_SetMaxStackSize (JSContext *ctx, size_t stack_size);
|
||||
/* should be called when changing thread to update the stack top value
|
||||
used to check stack overflow. */
|
||||
void JS_UpdateStackTop (JSContext *ctx);
|
||||
void JS_FreeRuntime (JSRuntime *rt);
|
||||
JS_BOOL JS_IsLiveObject (JSRuntime *rt, JSValue obj);
|
||||
|
||||
JSContext *JS_NewContext (JSRuntime *rt);
|
||||
void JS_FreeContext (JSContext *s);
|
||||
JSContext *JS_DupContext (JSContext *ctx);
|
||||
JSContext *JS_GetContext (JSRuntime *rt);
|
||||
void *JS_GetContextOpaque (JSContext *ctx);
|
||||
void JS_SetContextOpaque (JSContext *ctx, void *opaque);
|
||||
JSRuntime *JS_GetRuntime (JSContext *ctx);
|
||||
@@ -330,24 +317,6 @@ JSValue JS_GetClassProto (JSContext *ctx, JSClassID class_id);
|
||||
|
||||
JSContext *JS_NewContextWithHeapSize (JSRuntime *rt, size_t heap_size);
|
||||
|
||||
typedef struct JSMemoryUsage {
|
||||
int64_t malloc_size, malloc_limit, memory_used_size;
|
||||
int64_t malloc_count;
|
||||
int64_t memory_used_count;
|
||||
int64_t str_count, str_size;
|
||||
int64_t obj_count, obj_size;
|
||||
int64_t prop_count, prop_size;
|
||||
int64_t shape_count, shape_size;
|
||||
int64_t js_func_count, js_func_size, js_func_code_size;
|
||||
int64_t js_func_pc2line_count, js_func_pc2line_size;
|
||||
int64_t c_func_count, array_count;
|
||||
int64_t fast_array_count, fast_array_elements;
|
||||
int64_t binary_object_count, binary_object_size;
|
||||
} JSMemoryUsage;
|
||||
|
||||
void JS_ComputeMemoryUsage (JSRuntime *rt, JSMemoryUsage *s);
|
||||
void JS_DumpMemoryUsage (FILE *fp, const JSMemoryUsage *s, JSRuntime *rt);
|
||||
|
||||
typedef void JSClassFinalizer (JSRuntime *rt, JSValue val);
|
||||
typedef JSValue JSClassCall (JSContext *ctx, JSValue func_obj,
|
||||
JSValue this_val, int argc,
|
||||
@@ -522,7 +491,6 @@ static inline JSValue JS_NewString (JSContext *ctx, const char *str) {
|
||||
return JS_NewStringLen (ctx, str, strlen (str));
|
||||
}
|
||||
JSValue JS_ToString (JSContext *ctx, JSValue val);
|
||||
JSValue JS_ToPropertyKey (JSContext *ctx, JSValue val);
|
||||
const char *JS_ToCStringLen2 (JSContext *ctx, size_t *plen, JSValue val1, JS_BOOL cesu8);
|
||||
static inline const char * JS_ToCStringLen (JSContext *ctx, size_t *plen, JSValue val1) {
|
||||
return JS_ToCStringLen2 (ctx, plen, val1, 0);
|
||||
@@ -945,17 +913,6 @@ typedef void (*js_hook) (JSContext *, int type, js_debug *dbg, void *user);
|
||||
#define JS_HOOK_GC 8
|
||||
void js_debug_sethook (JSContext *ctx, js_hook, int type, void *user);
|
||||
|
||||
uint32_t js_debugger_stack_depth (JSContext *ctx);
|
||||
JSValue js_debugger_backtrace_fns (JSContext *ctx);
|
||||
JSValue js_debugger_closure_variables (JSContext *ctx, JSValue fn);
|
||||
JSValue js_debugger_local_variables (JSContext *ctx, int stack_index);
|
||||
void js_debugger_set_closure_variable (JSContext *js, JSValue fn,
|
||||
JSValue var_name, JSValue val);
|
||||
JSValue js_debugger_build_backtrace (JSContext *ctx);
|
||||
JSValue js_debugger_fn_info (JSContext *ctx, JSValue fn);
|
||||
JSValue js_debugger_fn_bytecode (JSContext *js, JSValue fn);
|
||||
void *js_debugger_val_address (JSContext *js, JSValue val);
|
||||
|
||||
/* Memory allocation functions (bump allocator) */
|
||||
void *js_malloc (JSContext *ctx, size_t size);
|
||||
void *js_mallocz (JSContext *ctx, size_t size);
|
||||
@@ -973,12 +930,6 @@ struct cJSON;
|
||||
/* Compiled bytecode (context-free, serializable) */
|
||||
typedef struct MachCode MachCode;
|
||||
|
||||
/* Compile AST cJSON tree to context-free MachCode. */
|
||||
MachCode *JS_CompileMachTree(struct cJSON *ast);
|
||||
|
||||
/* Compile AST JSON string to context-free MachCode. */
|
||||
MachCode *JS_CompileMach(const char *ast_json);
|
||||
|
||||
/* Free a compiled MachCode tree. */
|
||||
void JS_FreeMachCode(MachCode *mc);
|
||||
|
||||
|
||||
242
source/runtime.c
242
source/runtime.c
@@ -60,18 +60,6 @@ JS_BOOL JS_IsFunction (JSValue v) {
|
||||
return JS_IsGCObject(v) && objhdr_type(*chase(v)) == OBJ_FUNCTION;
|
||||
}
|
||||
|
||||
JS_BOOL JS_IsCode (JSValue v) {
|
||||
return JS_IsGCObject(v) && objhdr_type(*chase(v)) == OBJ_CODE;
|
||||
}
|
||||
|
||||
JS_BOOL JS_IsForwarded (JSValue v) {
|
||||
return JS_IsGCObject(v) && objhdr_type(*chase(v)) == OBJ_FORWARD;
|
||||
}
|
||||
|
||||
JS_BOOL JS_IsFrame (JSValue v) {
|
||||
return JS_IsGCObject(v) && objhdr_type(*chase(v)) == OBJ_FRAME;
|
||||
}
|
||||
|
||||
JS_BOOL JS_IsBlob (JSValue v) {
|
||||
return JS_IsGCObject(v) && objhdr_type(*chase(v)) == OBJ_BLOB;
|
||||
}
|
||||
@@ -1058,7 +1046,7 @@ JSValue gc_copy_value (JSContext *ctx, JSValue v, uint8_t *from_base, uint8_t *f
|
||||
continue;
|
||||
}
|
||||
|
||||
if (type != OBJ_ARRAY && type != OBJ_TEXT && type != OBJ_RECORD && type != OBJ_FUNCTION && type != OBJ_BLOB && type != OBJ_CODE && type != OBJ_FRAME) {
|
||||
if (type != OBJ_ARRAY && type != OBJ_TEXT && type != OBJ_RECORD && type != OBJ_FUNCTION && type != OBJ_BLOB && type != OBJ_FRAME) {
|
||||
fprintf (stderr, "gc_copy_value: invalid object type %d at %p (hdr=0x%llx)\n", type, ptr, (unsigned long long)hdr);
|
||||
fprintf (stderr, " This may be an interior pointer or corrupt root\n");
|
||||
fflush (stderr);
|
||||
@@ -1150,9 +1138,6 @@ void gc_scan_object (JSContext *ctx, void *ptr, uint8_t *from_base, uint8_t *fro
|
||||
case OBJ_BLOB:
|
||||
/* No internal references to scan */
|
||||
break;
|
||||
case OBJ_CODE:
|
||||
/* Dead code: old bytecode objects no longer created */
|
||||
break;
|
||||
case OBJ_FRAME: {
|
||||
/* JSFrame - scan function, caller, and slots */
|
||||
JSFrame *frame = (JSFrame *)ptr;
|
||||
@@ -1173,14 +1158,6 @@ void gc_scan_object (JSContext *ctx, void *ptr, uint8_t *from_base, uint8_t *fro
|
||||
}
|
||||
}
|
||||
|
||||
/* gc_scan_parser_cpool - no-op now that the old compiler is removed.
|
||||
current_parse_fd is always NULL without cell_js.c. */
|
||||
void gc_scan_parser_cpool (JSContext *ctx, uint8_t *from_base, uint8_t *from_end,
|
||||
uint8_t *to_base, uint8_t **to_free, uint8_t *to_end) {
|
||||
(void)ctx; (void)from_base; (void)from_end;
|
||||
(void)to_base; (void)to_free; (void)to_end;
|
||||
}
|
||||
|
||||
/* Cheney copying GC - collect garbage and compact live objects
|
||||
allow_grow: if true, grow heap when recovery is poor
|
||||
alloc_size: the allocation that triggered GC — used to size the new block */
|
||||
@@ -1282,11 +1259,6 @@ int ctx_gc (JSContext *ctx, int allow_grow, size_t alloc_size) {
|
||||
ref->val = gc_copy_value (ctx, ref->val, from_base, from_end, to_base, &to_free, to_end);
|
||||
}
|
||||
|
||||
/* Scan parser's cpool (if parsing is in progress) */
|
||||
if (ctx->current_parse_fd) {
|
||||
gc_scan_parser_cpool (ctx, from_base, from_end, to_base, &to_free, to_end);
|
||||
}
|
||||
|
||||
/* Cheney scan: scan copied objects to find more references */
|
||||
uint8_t *scan = to_base;
|
||||
#ifdef DUMP_GC_DETAIL
|
||||
@@ -1530,14 +1502,6 @@ void JS_FreeContext (JSContext *ctx) {
|
||||
JSRuntime *rt = ctx->rt;
|
||||
int i;
|
||||
|
||||
#ifdef DUMP_MEM
|
||||
{
|
||||
JSMemoryUsage stats;
|
||||
JS_ComputeMemoryUsage (rt, &stats);
|
||||
JS_DumpMemoryUsage (stdout, &stats, rt);
|
||||
}
|
||||
#endif
|
||||
|
||||
for (i = 0; i < JS_NATIVE_ERROR_COUNT; i++) {
|
||||
}
|
||||
for (i = 0; i < ctx->class_count; i++) {
|
||||
@@ -2295,18 +2259,6 @@ JSValue JS_NewObject (JSContext *ctx) {
|
||||
}
|
||||
|
||||
|
||||
// TODO: needs reworked
|
||||
int js_method_set_properties (JSContext *ctx, JSValue func_obj, JSValue name, int flags, JSValue home_obj) {
|
||||
(void)ctx;
|
||||
(void)flags;
|
||||
(void)home_obj;
|
||||
if (!JS_IsFunction (func_obj)) return -1;
|
||||
JSFunction *f = JS_VALUE_GET_FUNCTION (func_obj);
|
||||
/* name is now JSValue text */
|
||||
if (JS_IsText (name)) { f->name = name; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Note: at least 'length' arguments will be readable in 'argv' */
|
||||
static JSValue JS_NewCFunction3 (JSContext *ctx, JSCFunction *func, const char *name, int length, JSCFunctionEnum cproto, int magic) {
|
||||
JSValue func_obj;
|
||||
@@ -2454,7 +2406,7 @@ static int js_intrinsic_array_set (JSContext *ctx, JSValue *arr_ptr, word_t idx,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Allocate intrinsic function (JS_TAG_FUNCTION) */
|
||||
/* Allocate a new function object */
|
||||
JSValue js_new_function (JSContext *ctx, JSFunctionKind kind) {
|
||||
JSFunction *func = js_mallocz (ctx, sizeof (JSFunction));
|
||||
if (!func) return JS_EXCEPTION;
|
||||
@@ -2465,17 +2417,6 @@ JSValue js_new_function (JSContext *ctx, JSFunctionKind kind) {
|
||||
return JS_MKPTR (func);
|
||||
}
|
||||
|
||||
/* Get pointer to an upvalue in outer scope frame chain.
|
||||
depth=0 is current frame, depth=1 is immediate outer, etc.
|
||||
Returns NULL if depth exceeds the frame chain.
|
||||
frame_val is a JSValue containing a JSFrame pointer. */
|
||||
|
||||
void JS_ComputeMemoryUsage (JSRuntime *rt, JSMemoryUsage *s) {
|
||||
}
|
||||
|
||||
void JS_DumpMemoryUsage (FILE *fp, const JSMemoryUsage *s, JSRuntime *rt) {
|
||||
}
|
||||
|
||||
/* WARNING: obj is freed */
|
||||
JSValue JS_Throw (JSContext *ctx, JSValue obj) {
|
||||
ctx->current_exception = obj;
|
||||
@@ -3080,39 +3021,6 @@ int JS_DeletePropertyKey (JSContext *ctx, JSValue obj, JSValue key) {
|
||||
Note: makes assumption about the bit pattern of the flags
|
||||
*/
|
||||
|
||||
/* return TRUE if 'obj' has a non empty 'name' string */
|
||||
static BOOL js_object_has_name (JSContext *ctx, JSValue obj) {
|
||||
if (JS_VALUE_GET_TAG (obj) != JS_TAG_PTR) return FALSE;
|
||||
JSRecord *rec = (JSRecord *)JS_VALUE_GET_OBJ (obj);
|
||||
JSValue name_key = MIST_TryNewImmediateASCII ("name", 4);
|
||||
int slot = rec_find_slot (rec, name_key);
|
||||
if (slot <= 0) return FALSE;
|
||||
JSValue val = rec->slots[slot].val;
|
||||
if (!JS_IsText (val)) return TRUE; /* has name but it's not a string = truthy */
|
||||
return (js_string_value_len (val) != 0);
|
||||
}
|
||||
|
||||
int JS_DefineObjectName (JSContext *ctx, JSValue obj, JSValue name) {
|
||||
if (!JS_IsNull (name) && JS_IsGCObject (obj)
|
||||
&& !js_object_has_name (ctx, obj)) {
|
||||
JSValue name_key = MIST_TryNewImmediateASCII ("name", 4);
|
||||
if (JS_SetPropertyInternal (ctx, obj, name_key, name)
|
||||
< 0)
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int JS_DefineObjectNameComputed (JSContext *ctx, JSValue obj, JSValue str) {
|
||||
if (JS_IsGCObject (obj) && !js_object_has_name (ctx, obj)) {
|
||||
JSValue name_key = MIST_TryNewImmediateASCII ("name", 4);
|
||||
if (JS_SetPropertyInternal (ctx, obj, name_key, str)
|
||||
< 0)
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int JS_DeleteProperty (JSContext *ctx, JSValue obj, JSValue prop) {
|
||||
JSRecord *rec;
|
||||
int slot;
|
||||
@@ -4023,9 +3931,6 @@ __maybe_unused void JS_DumpGCObject (JSRuntime *rt,
|
||||
JS_DumpObject (rt, (JSRecord *)p);
|
||||
} else {
|
||||
switch (objhdr_type (*p)) {
|
||||
case OBJ_CODE:
|
||||
printf ("[function bytecode]");
|
||||
break;
|
||||
case OBJ_ARRAY:
|
||||
printf ("[array]");
|
||||
break;
|
||||
@@ -4091,73 +3996,10 @@ BOOL JS_StrictEq (JSContext *ctx, JSValue op1, JSValue op2) {
|
||||
return js_strict_eq (ctx, op1, op2);
|
||||
}
|
||||
|
||||
__exception int js_operator_delete (JSContext *ctx, JSValue *sp) {
|
||||
JSValue op1, op2;
|
||||
int ret;
|
||||
|
||||
op1 = sp[-2];
|
||||
op2 = sp[-1];
|
||||
|
||||
ret = JS_DeletePropertyKey (ctx, op1, op2);
|
||||
if (unlikely (ret < 0)) return -1;
|
||||
sp[-2] = JS_NewBool (ctx, ret);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* XXX: not 100% compatible, but mozilla seems to use a similar
|
||||
implementation to ensure that caller in non strict mode does not
|
||||
throw (ES5 compatibility) */
|
||||
static JSValue js_throw_type_error (JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
|
||||
return JS_ThrowTypeError (ctx, "invalid property access");
|
||||
}
|
||||
|
||||
|
||||
__exception int JS_CopyDataProperties (JSContext *ctx, JSValue target, JSValue source, JSValue excluded, BOOL setprop) {
|
||||
JSValue keys, key, val;
|
||||
uint32_t i, key_count;
|
||||
int ret;
|
||||
|
||||
if (JS_VALUE_GET_TAG (source) != JS_TAG_PTR) return 0;
|
||||
|
||||
/* Get all string keys from source */
|
||||
keys = JS_GetOwnPropertyNames (ctx, source);
|
||||
if (JS_IsException (keys)) return -1;
|
||||
if (js_get_length32 (ctx, &key_count, keys)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < key_count; i++) {
|
||||
key = JS_GetPropertyNumber (ctx, keys, i);
|
||||
if (JS_IsException (key)) goto exception;
|
||||
|
||||
/* Check if key is excluded */
|
||||
if (JS_VALUE_GET_TAG (excluded) == JS_TAG_PTR) {
|
||||
/* Check if key exists in excluded object */
|
||||
JSValue test = JS_GetProperty (ctx, excluded, key);
|
||||
if (!JS_IsNull (test) && !JS_IsException (test)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
/* Get property value from source */
|
||||
val = JS_GetProperty (ctx, source, key);
|
||||
if (JS_IsException (val)) {
|
||||
goto exception;
|
||||
}
|
||||
|
||||
/* Set property on target */
|
||||
ret = JS_SetProperty (ctx, target, key, val);
|
||||
if (ret < 0) goto exception;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
exception:
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
JSValue js_call_c_function (JSContext *ctx, JSValue func_obj, JSValue this_obj, int argc, JSValue *argv) {
|
||||
JSCFunctionType func;
|
||||
JSFunction *f;
|
||||
@@ -4300,12 +4142,6 @@ JSValue JS_Call (JSContext *ctx, JSValue func_obj, JSValue this_obj, int argc, J
|
||||
/*******************************************************************/
|
||||
/* runtime functions & objects */
|
||||
|
||||
int check_function (JSContext *ctx, JSValue obj) {
|
||||
if (likely (JS_IsFunction (obj))) return 0;
|
||||
JS_ThrowTypeError (ctx, "not a function");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int check_exception_free (JSContext *ctx, JSValue obj) {
|
||||
return JS_IsException (obj);
|
||||
}
|
||||
@@ -4402,7 +4238,7 @@ __exception int js_get_length32 (JSContext *ctx, uint32_t *pres, JSValue obj) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (tag == JS_TAG_FUNCTION) {
|
||||
if (JS_IsFunction (obj)) {
|
||||
JSFunction *fn = JS_VALUE_GET_FUNCTION (obj);
|
||||
*pres = fn->length;
|
||||
return 0;
|
||||
@@ -10786,78 +10622,6 @@ void js_debug_sethook (JSContext *ctx, js_hook hook, int type, void *user) {
|
||||
ctx->trace_data = user;
|
||||
}
|
||||
|
||||
uint32_t js_debugger_stack_depth (JSContext *ctx) {
|
||||
return ctx->stack_depth;
|
||||
}
|
||||
|
||||
/* return an array of the actual fn objects as a backtrace */
|
||||
JSValue js_debugger_backtrace_fns (JSContext *ctx) {
|
||||
/* JSValue ret = JS_NewArray (ctx);
|
||||
uint32_t stack_index = 0;
|
||||
|
||||
for (sf = ctx->current_stack_frame; sf != NULL; sf = sf->prev_frame) {
|
||||
uint32_t id = stack_index++;
|
||||
JS_SetPropertyNumber (ctx, ret, id, sf->cur_func);
|
||||
}
|
||||
return ret;
|
||||
*/
|
||||
}
|
||||
|
||||
/* build a backtrace of info for printing */
|
||||
JSValue js_debugger_build_backtrace (JSContext *ctx) {
|
||||
/*
|
||||
JSStackFrame *sf;
|
||||
JSValue ret = JS_NewArray (ctx);
|
||||
uint32_t stack_index = 0;
|
||||
|
||||
for (sf = ctx->current_stack_frame; sf != NULL; sf = sf->prev_frame) {
|
||||
JSValue current_frame = JS_NewObject (ctx);
|
||||
uint32_t id = stack_index++;
|
||||
JS_SetPropertyStr (ctx, current_frame, "id", JS_NewUint32 (ctx, id));
|
||||
|
||||
const char *func_name_str = get_func_name (ctx, sf->cur_func);
|
||||
if (!func_name_str || func_name_str[0] == '\0')
|
||||
JS_SetPropertyStr (ctx, current_frame, "name", JS_NewString (ctx, "<anonymous>"));
|
||||
else
|
||||
JS_SetPropertyStr (ctx, current_frame, "name", JS_NewString (ctx, func_name_str));
|
||||
JS_FreeCString (ctx, func_name_str);
|
||||
|
||||
JS_SetPropertyNumber (ctx, ret, id, current_frame);
|
||||
}
|
||||
return ret;
|
||||
*/
|
||||
}
|
||||
|
||||
JSValue js_debugger_fn_info (JSContext *ctx, JSValue fn) {
|
||||
(void)fn;
|
||||
return JS_NewObject (ctx);
|
||||
}
|
||||
|
||||
JSValue js_debugger_fn_bytecode (JSContext *ctx, JSValue fn) {
|
||||
(void)fn;
|
||||
return JS_NewArray (ctx);
|
||||
}
|
||||
|
||||
JSValue js_debugger_local_variables (JSContext *ctx, int stack_index) {
|
||||
(void)stack_index;
|
||||
return JS_NewObject (ctx);
|
||||
}
|
||||
|
||||
void js_debugger_set_closure_variable (JSContext *ctx, JSValue fn, JSValue var_name, JSValue val) {
|
||||
/* TODO: Reimplement using outer_frame mechanism if debugging is needed */
|
||||
(void)ctx; (void)fn; (void)var_name; (void)val;
|
||||
}
|
||||
|
||||
JSValue js_debugger_closure_variables (JSContext *ctx, JSValue fn) {
|
||||
/* TODO: Reimplement using outer_frame mechanism if debugging is needed */
|
||||
(void)fn;
|
||||
return JS_NewObject (ctx);
|
||||
}
|
||||
|
||||
void *js_debugger_val_address (JSContext *ctx, JSValue val) {
|
||||
return JS_VALUE_GET_PTR (val);
|
||||
}
|
||||
|
||||
/* ============================================================================
|
||||
* Cell Script Module: json
|
||||
* Provides json.encode() and json.decode() using pure C implementation
|
||||
|
||||
Reference in New Issue
Block a user