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

  • From: Gus Caplan <fluffyrobotcheese@xxxxxxxxx>
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Thu, 5 Sep 2019 08:53:21 -0500

In other js engines, the native APIs tend to stick the original behaviour,
as they don't usually call functions at all, they're just using the
engine's abilities.

On Thu, Sep 5, 2019, 05:28 Ondřej Jirman <deibeemoocheuguhumoh@xxxxxx>
wrote:

Hi,

On Thu, Sep 05, 2019 at 10:01:09AM +0200, Saúl Ibarra Corretgé wrote:
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.

JS has quite a number of functions in its "standard library". Object.freeze
is just one of them. These can be "overriden" or replaced, if you will, by
user
code.

In C API you may want to call the original function or the user provided
one.
There's no telling which one, in general.

The API you suggested will always call the original function. It may or may
not be what the user of the C API expects. The application as a whole may
want either behavior.

For example you'll no longer be able to do:

Object.freeze = function(o) {
        return o;
};

to disable freezing globally, or to modify Object.freeze function to add
logging on what objects are being freezed globally for quick debugging.

You may want the behavior you suggested for the C API, but is it desirable
in general?

regards,
        o.


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: