Skip to content

Commit

Permalink
updated patched quickjs files
Browse files Browse the repository at this point in the history
  • Loading branch information
abner authored Aug 25, 2020
1 parent ad95af0 commit e39d47d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 7 deletions.
98 changes: 92 additions & 6 deletions quickjs/src/main/c/quickjs/quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ enum {
JS_CLASS_BYTECODE_FUNCTION, /* u.func */
JS_CLASS_BOUND_FUNCTION, /* u.bound_function */
JS_CLASS_C_FUNCTION_DATA, /* u.c_function_data_record */
JS_CLASS_C_CLOSURE, /* u.c_closure_record */
JS_CLASS_GENERATOR_FUNCTION, /* u.func */
JS_CLASS_FOR_IN_ITERATOR, /* u.for_in_iterator */
JS_CLASS_REGEXP, /* u.regexp */
Expand Down Expand Up @@ -872,6 +873,7 @@ struct JSObject {
void *opaque;
struct JSBoundFunction *bound_function; /* JS_CLASS_BOUND_FUNCTION */
struct JSCFunctionDataRecord *c_function_data_record; /* JS_CLASS_C_FUNCTION_DATA */
struct JSCClosureRecord *c_closure_record; /* JS_CLASS_C_CLOSURE */
struct JSForInIterator *for_in_iterator; /* JS_CLASS_FOR_IN_ITERATOR */
struct JSArrayBuffer *array_buffer; /* JS_CLASS_ARRAY_BUFFER, JS_CLASS_SHARED_ARRAY_BUFFER */
struct JSTypedArray *typed_array; /* JS_CLASS_UINT8C_ARRAY..JS_CLASS_DATAVIEW */
Expand Down Expand Up @@ -1220,6 +1222,10 @@ static void js_c_function_data_mark(JSRuntime *rt, JSValueConst val,
static JSValue js_c_function_data_call(JSContext *ctx, JSValueConst func_obj,
JSValueConst this_val,
int argc, JSValueConst *argv, int flags);
static void js_c_closure_finalizer(JSRuntime *rt, JSValue val);
static JSValue js_c_closure_call(JSContext *ctx, JSValueConst func_obj,
JSValueConst this_val,
int argc, JSValueConst *argv, int flags);
static JSAtom js_symbol_to_atom(JSContext *ctx, JSValue val);
static void add_gc_object(JSRuntime *rt, JSGCObjectHeader *h,
JSGCObjectTypeEnum type);
Expand Down Expand Up @@ -1438,6 +1444,7 @@ static JSClassShortDef const js_std_class_def[] = {
{ JS_ATOM_Function, js_bytecode_function_finalizer, js_bytecode_function_mark }, /* JS_CLASS_BYTECODE_FUNCTION */
{ JS_ATOM_Function, js_bound_function_finalizer, js_bound_function_mark }, /* JS_CLASS_BOUND_FUNCTION */
{ JS_ATOM_Function, js_c_function_data_finalizer, js_c_function_data_mark }, /* JS_CLASS_C_FUNCTION_DATA */
{ JS_ATOM_Function, js_c_closure_finalizer, NULL}, /* JS_CLASS_C_CLOSURE */
{ JS_ATOM_GeneratorFunction, js_bytecode_function_finalizer, js_bytecode_function_mark }, /* JS_CLASS_GENERATOR_FUNCTION */
{ JS_ATOM_ForInIterator, js_for_in_iterator_finalizer, js_for_in_iterator_mark }, /* JS_CLASS_FOR_IN_ITERATOR */
{ JS_ATOM_RegExp, js_regexp_finalizer, NULL }, /* JS_CLASS_REGEXP */
Expand Down Expand Up @@ -1551,7 +1558,7 @@ static void set_dummy_numeric_ops(JSNumericOperations *ops)

#endif /* CONFIG_BIGNUM */

#if !defined(CONFIG_STACK_CHECK)
#if 1 //!defined(CONFIG_STACK_CHECK)
/* no stack limitation */
static inline uint8_t *js_get_stack_pointer(void)
{
Expand Down Expand Up @@ -1628,6 +1635,7 @@ JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque)

rt->class_array[JS_CLASS_C_FUNCTION].call = js_call_c_function;
rt->class_array[JS_CLASS_C_FUNCTION_DATA].call = js_c_function_data_call;
rt->class_array[JS_CLASS_C_CLOSURE].call = js_c_closure_call;
rt->class_array[JS_CLASS_BOUND_FUNCTION].call = js_call_bound_function;
rt->class_array[JS_CLASS_GENERATOR_FUNCTION].call = js_generator_function_call;
if (init_shape_hash(rt))
Expand Down Expand Up @@ -1663,10 +1671,10 @@ static inline size_t js_def_malloc_usable_size(void *ptr)
#elif defined(EMSCRIPTEN)
return 0;
#elif defined(__linux__)
return 0; //malloc_usable_size(ptr);
return 0; //return malloc_usable_size(ptr);
#else
/* change this to `return 0;` if compilation fails */
return 0; //malloc_usable_size(ptr);
return malloc_usable_size(ptr);
#endif
}

Expand Down Expand Up @@ -1737,10 +1745,10 @@ static const JSMallocFunctions def_malloc_funcs = {
#elif defined(EMSCRIPTEN)
NULL,
#elif defined(__linux__)
NULL, //(size_t (*)(const void *))malloc_usable_size,
NULL, //(size_t (*)(const void *))malloc_usable_size,
#else
/* change this to `NULL,` if compilation fails */
NULL, //malloc_usable_size,
malloc_usable_size,
#endif
};

Expand Down Expand Up @@ -5107,6 +5115,75 @@ static void js_autoinit_mark(JSRuntime *rt, JSProperty *pr,
mark_func(rt, &js_autoinit_get_realm(pr)->header);
}

typedef struct JSCClosureRecord {
JSCClosure *func;
uint16_t length;
uint16_t magic;
void *opaque;
void (*opaque_finalize)(void*);
} JSCClosureRecord;

static void js_c_closure_finalizer(JSRuntime *rt, JSValue val)
{
JSCClosureRecord *s = JS_GetOpaque(val, JS_CLASS_C_CLOSURE);

if (s) {
if (s->opaque_finalize)
s->opaque_finalize(s->opaque);

js_free_rt(rt, s);
}
}

static JSValue js_c_closure_call(JSContext *ctx, JSValueConst func_obj,
JSValueConst this_val,
int argc, JSValueConst *argv, int flags)
{
JSCClosureRecord *s = JS_GetOpaque(func_obj, JS_CLASS_C_CLOSURE);
JSValueConst *arg_buf;
int i;

/* XXX: could add the function on the stack for debug */
if (unlikely(argc < s->length)) {
arg_buf = alloca(sizeof(arg_buf[0]) * s->length);
for(i = 0; i < argc; i++)
arg_buf[i] = argv[i];
for(i = argc; i < s->length; i++)
arg_buf[i] = JS_UNDEFINED;
} else {
arg_buf = argv;
}

return s->func(ctx, this_val, argc, arg_buf, s->magic, s->opaque);
}

JSValue JS_NewCClosure(JSContext *ctx, JSCClosure *func,
int length, int magic, void *opaque,
void (*opaque_finalize)(void*))
{
JSCClosureRecord *s;
JSValue func_obj;

func_obj = JS_NewObjectProtoClass(ctx, ctx->function_proto,
JS_CLASS_C_CLOSURE);
if (JS_IsException(func_obj))
return func_obj;
s = js_malloc(ctx, sizeof(*s));
if (!s) {
JS_FreeValue(ctx, func_obj);
return JS_EXCEPTION;
}
s->func = func;
s->length = length;
s->magic = magic;
s->opaque = opaque;
s->opaque_finalize = opaque_finalize;
JS_SetOpaque(func_obj, s);
js_function_set_properties(ctx, func_obj,
JS_ATOM_empty_string, length);
return func_obj;
}

static void free_property(JSRuntime *rt, JSProperty *pr, int prop_flags)
{
if (unlikely(prop_flags & JS_PROP_TMASK)) {
Expand Down Expand Up @@ -6018,6 +6095,15 @@ void JS_ComputeMemoryUsage(JSRuntime *rt, JSMemoryUsage *s)
}
}
break;
case JS_CLASS_C_CLOSURE: /* u.c_closure_record */
{
JSCClosureRecord *c = p->u.c_closure_record;
if (c) {
s->memory_used_count += 1;
s->memory_used_size += sizeof(*c);
}
}
break;
case JS_CLASS_REGEXP: /* u.regexp */
compute_jsstring_size(p->u.regexp.pattern, hp);
compute_jsstring_size(p->u.regexp.bytecode, hp);
Expand Down Expand Up @@ -53309,4 +53395,4 @@ void JS_AddIntrinsicTypedArrays(JSContext *ctx)
#ifdef CONFIG_ATOMICS
JS_AddIntrinsicAtomics(ctx);
#endif
}
}
6 changes: 5 additions & 1 deletion quickjs/src/main/c/quickjs/quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ static inline JS_BOOL JS_VALUE_IS_NAN(JSValue v)
typedef JSValue JSCFunction(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
typedef JSValue JSCFunctionMagic(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic);
typedef JSValue JSCFunctionData(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic, JSValue *func_data);
typedef JSValue JSCClosure(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic, void *opaque);

typedef struct JSMallocState {
size_t malloc_count;
Expand Down Expand Up @@ -925,6 +926,9 @@ JSValue JS_NewCFunction2(JSContext *ctx, JSCFunction *func,
JSValue JS_NewCFunctionData(JSContext *ctx, JSCFunctionData *func,
int length, int magic, int data_len,
JSValueConst *data);
JSValue JS_NewCClosure(JSContext *ctx, JSCClosure *func,
int length, int magic, void *opaque,
void (*opaque_finalize)(void*));

static inline JSValue JS_NewCFunction(JSContext *ctx, JSCFunction *func, const char *name,
int length)
Expand Down Expand Up @@ -1027,4 +1031,4 @@ int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m,
} /* extern "C" { */
#endif

#endif /* QUICKJS_H */
#endif /* QUICKJS_H */

0 comments on commit e39d47d

Please sign in to comment.