rscarson/rustyscript

Make `rustyscript` global object optional & able to be renamed

ac-z opened this issue · 2 comments

ac-z commented

Right now, registered functions are made available under the functions and async_functions proxies within the global rustyscript object, alongside the essential functions bail and register_entrypoint.

This is a good default, but I think it would be good to allow more flexibility here. I have the following additions to the API in mind:

  • An optional "runtime name" in RuntimeOptions which determines the name of the global object. If None, default to the current name, rustyscript.
  • Optional exclusion of these lines, either via a feature flag or a boolean in RuntimeOptions. This is important for sufficiently complex use cases, which may need to organize many ops into categories (e.g. appname.category.subcategory.foo())
    // Populate the global object
    globalThis.rustyscript = {
    'register_entrypoint': (f) => Deno.core.ops.op_register_entrypoint(f),
    'bail': (msg) => { throw new Error(msg) },
    'functions': new Proxy({}, {
    get: function(_target, name) {
    return (...args) => Deno.core.ops.call_registered_function(name, args);
    }
    }),
    'async_functions': new Proxy({}, {
    get: function(_target, name) {
    return (...args) => Deno.core.ops.call_registered_function_async(name, args);
    }
    })
    };
    Object.freeze(globalThis.rustyscript);
    The best way to avoid making this overly complex is to simply allow users implement their own replacement for this bit of code with their own extension.
    • Maybe this code snippet should be its own tiny extension, outside of rustyscript.js? Then it could be handled like every other optional extension.

Things to consider

  • There are a bunch of places in the docs we'd have to diligently change.
  • If the global object is turned off, the "runtime name" option in RuntimeOptions wouldn't do anything. Perhaps this is a point in favor of using a feature flag, as we can ensure that option is only available when there's a global object to be named in the first place.
  • RuntimeOptions is getting crowded.

I really don't see the point
It's JavaScript, users can just rename it, or make their own object referencing mine

This would just add complexity

Users could just as easily:

globalThis.myFunctions = {
    rustyscript.functions.a, rustyscript.functions.b
}
ac-z commented

I really don't see the point It's JavaScript, users can just rename it, or make their own object referencing mine

Was under the impression that freezing in JavaScript doesn't let you delete or rename. Closing.