[quickjs-devel] [PATCH] Add ability to freeze objects from C

  • From: Saúl Ibarra Corretgé <s@xxxxxxxxxx>
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Thu, 5 Sep 2019 10:01:09 +0200

Hey there,

I've come to need to freeze an abject that I'm exposing on a C module
(think of scriptArgs). I don't want that object to be changeable, so I'd
like to Object.freeze it, but from C.

The attached patch does just that. I hope it (or a variant of it) can be
included in a future release.


Cheers,

-- 
Saúl
diff --git a/deps/quickjs/include/quickjs.h b/deps/quickjs/include/quickjs.h
index 206516b..e6e5959 100644
--- a/deps/quickjs/include/quickjs.h
+++ b/deps/quickjs/include/quickjs.h
@@ -903,6 +903,8 @@ int JS_SetModuleExport(JSContext *ctx, JSModuleDef *m, 
const char *export_name,
 int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m,
                            const JSCFunctionListEntry *tab, int len);
 
+JSValue JS_ObjectFreeze(JSContext *ctx, JSValue obj);
+
 #undef js_unlikely
 #undef js_force_inline
 
diff --git a/deps/quickjs/src/quickjs.c b/deps/quickjs/src/quickjs.c
index d5c1c83..08146d6 100644
--- a/deps/quickjs/src/quickjs.c
+++ b/deps/quickjs/src/quickjs.c
@@ -50153,3 +50153,7 @@ void JS_AddIntrinsicTypedArrays(JSContext *ctx)
     JS_AddIntrinsicAtomics(ctx);
 #endif
 }
+
+JSValue JS_ObjectFreeze(JSContext *ctx, JSValue obj) {
+    return js_object_seal(ctx, JS_UNDEFINED, 1, (JSValueConst *)&obj, 1);
+}

Other related posts: