Skip to content

Commit

Permalink
add JS_{Get,Set}RuntimeOpaqueValue
Browse files Browse the repository at this point in the history
this is where we'll put worker global init callbacks
  • Loading branch information
suchipi committed Sep 3, 2024
1 parent be61a31 commit 64d0cd8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
36 changes: 25 additions & 11 deletions src/quickjs/quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ struct JSRuntime {
uint32_t operator_count;
#endif
void *user_opaque;
JSValue user_opaque_val;
};

struct JSClass {
Expand Down Expand Up @@ -1593,6 +1594,7 @@ JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque)
JS_UpdateStackTop(rt);

rt->current_exception = JS_NULL;
rt->user_opaque_val = JS_NULL;

return rt;
fail:
Expand All @@ -1610,6 +1612,17 @@ void JS_SetRuntimeOpaque(JSRuntime *rt, void *opaque)
rt->user_opaque = opaque;
}

void JS_SetRuntimeOpaqueValue(JSRuntime *rt, JSValue value)
{
rt->user_opaque_val = value;
}

/* NOTE: you must free it! */
JSValue JS_GetRuntimeOpaqueValue(JSRuntime *rt)
{
return JS_DupValueRT(rt, rt->user_opaque_val);
}

/* default memory allocation functions with memory limitation */
static inline size_t js_def_malloc_usable_size(void *ptr)
{
Expand Down Expand Up @@ -1867,6 +1880,7 @@ void JS_FreeRuntime(JSRuntime *rt)
int i;

JS_FreeValueRT(rt, rt->current_exception);
JS_FreeValueRT(rt, rt->user_opaque_val);

list_for_each_safe(el, el1, &rt->job_list) {
JSJobEntry *e = list_entry(el, JSJobEntry, link);
Expand Down Expand Up @@ -2118,6 +2132,17 @@ void JS_SetContextOpaque(JSContext *ctx, void *opaque)
ctx->user_opaque = opaque;
}

void JS_SetContextOpaqueValue(JSContext *ctx, JSValue value)
{
ctx->user_opaque_val = value;
}

/* NOTE: you must free it! */
JSValue JS_GetContextOpaqueValue(JSContext *ctx)
{
return JS_DupValue(ctx, ctx->user_opaque_val);
}

/* set the new value and free the old value after (freeing the value
can reallocate the object data) */
static inline void set_value(JSContext *ctx, JSValue *pval, JSValue new_val)
Expand Down Expand Up @@ -27101,17 +27126,6 @@ void *JS_GetModuleLoaderOpaque(JSRuntime *rt)
return rt->module_loader_opaque;
}

void JS_SetContextOpaqueValue(JSContext *ctx, JSValue value)
{
ctx->user_opaque_val = value;
}

/* NOTE: you must free it! */
JSValue JS_GetContextOpaqueValue(JSContext *ctx)
{
return JS_DupValue(ctx, ctx->user_opaque_val);
}

/* default module filename normalizer */
static char *js_default_module_normalize_name(JSContext *ctx,
const char *base_name,
Expand Down
10 changes: 6 additions & 4 deletions src/quickjs/quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,9 @@ JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque);
void JS_FreeRuntime(JSRuntime *rt);
void *JS_GetRuntimeOpaque(JSRuntime *rt);
void JS_SetRuntimeOpaque(JSRuntime *rt, void *opaque);
void JS_SetRuntimeOpaqueValue(JSRuntime *rt, JSValue value);
/* NOTE: you must free it! */
JSValue JS_GetRuntimeOpaqueValue(JSRuntime *rt);
typedef void JS_MarkFunc(JSRuntime *rt, JSGCObjectHeader *gp);
void JS_MarkValue(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_func);
void JS_RunGC(JSRuntime *rt);
Expand All @@ -420,6 +423,9 @@ void JS_FreeContext(JSContext *s);
JSContext *JS_DupContext(JSContext *ctx);
void *JS_GetContextOpaque(JSContext *ctx);
void JS_SetContextOpaque(JSContext *ctx, void *opaque);
void JS_SetContextOpaqueValue(JSContext *ctx, JSValue value);
/* NOTE: you must free it! */
JSValue JS_GetContextOpaqueValue(JSContext *ctx);
JSRuntime *JS_GetRuntime(JSContext *ctx);
void JS_SetClassProto(JSContext *ctx, JSClassID class_id, JSValue obj);
JSValue JS_GetClassProto(JSContext *ctx, JSClassID class_id);
Expand Down Expand Up @@ -993,10 +999,6 @@ void JS_SetModuleLoaderOpaque(JSRuntime *rt, void *opaque);
/* return the value set by JS_SetModuleLoaderOpaque. could be NULL. */
void *JS_GetModuleLoaderOpaque(JSRuntime *rt);

void JS_SetContextOpaqueValue(JSContext *ctx, JSValue value);
/* NOTE: you must free it! */
JSValue JS_GetContextOpaqueValue(JSContext *ctx);

/* return the import.meta object of a module. you'll have to free it when done */
JSValue JS_GetImportMeta(JSContext *ctx, JSModuleDef *m);
JSAtom JS_GetModuleName(JSContext *ctx, JSModuleDef *m);
Expand Down

0 comments on commit 64d0cd8

Please sign in to comment.