Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API method to set property setter #13

Closed
dug9 opened this issue Jun 8, 2014 · 2 comments
Closed

API method to set property setter #13

dug9 opened this issue Jun 8, 2014 · 2 comments

Comments

@dug9
Copy link

dug9 commented Jun 8, 2014

Q.is there a duktape API method for setting a C function setter and getter on a property?
Or can you direct me to some sample code if it takes a compound series of API calls?
Or how would I do it in the prototype constructor in js?
Or where would I hack in duktape.c to add one?
Thanks for any tips, links, thoughts.
-Doug
more..
I'm working on a platform port of freewrl.sourceforge.net virtual reality player (like a game engine) to windows 8.1 app store (versus desktoo). freewrl win32 desktop currently uses spidermonkey/libmozjs 185 - lovely, nice Class instancing API, including detailed property definition,
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/JSAPI_Cookbook
/* JSAPI */
if (!JS_DefineProperty(cx, obj, "prop", JSVAL_VOID,
(JSPropertyOp) GetPropFunc, (JSStrictPropertyOp) SetPropFunc,
JSPROP_SHARED | JSPROP_NATIVE_ACCESSORS | JSPROP_ENUMERATE)) {
return JS_FALSE;
}
which includes giving non-default C getters/setters on properties, Porting SM or V8 is more than I want to do, and I don't see scripting engines available for the platform -ms js engine appears to be unavailable. Windows app store 8.1 API breaks a lot of opensource libraries. I've worked through them all for freewrl except the javascript engine..
more..
Duktape / ecma5.1 is perhaps sufficient for what we need -I'm going through now to see how to port from SM to duktape. For our case all js is run from one thread -like a game engine looping, at one point in the loop we go through all the script contexts running functions one at a time. Each function is defined at the top level in the context, and the scene/.js author has to name their function the same as their variable defined outside the function, so we know what function to run.
http://www.web3d.org/files/specifications/19775-1/V3.3/Part01/components/scripting.html#Script

  • the url field contains url to a .js file, or is a js string blob
    http://dug9.users.sourceforge.net/web3d/tests/Javascript_tests/touch.wrl
  • shows a sample complete vrml file, with a script node, and script text in url field.
    When initializing each context we add about 20 of our Classes/object prototypes, and twin native instances of these types in the js context at the top level, as needed for the Script Node.
    The scripts change variables held in C code via the C getters and setters (or for ecma primitive types, we copy in and out of the context before and after running the script functions)
    more..
    windows 8.1 app store -also know as winRT, or more recently just 'windows' vs windows-desktop- exposes a subset of the windows-desktop api, and implements sandboxing (fopen/fread work but only on your appdata and app installation folders), and there's no stdout/console. (Sandboxing is becoming important in business environments, to overcome 'download reluctance' for opensource apps.) Many old favorite functions are missing in the windows store app API. It breaks old posix libs like pthread for windows, there's no opengl32.dll, and compiler warnings in desktop are promoted to errors in windows app store. I worked for months and got freewrl working except for the javascript engine part. I'd love to get that working.
    more...
    libmozjs/spidermonkey uses an autoconfig the depends on some mozilla developer environment being installed, and that's broken for the windows store. V8 uses gyp to generate build files, but not for windows store, v8 depends on cygwin/mingw. That's all broken for windows store. Duktape needed a few tweeks in the code to stop the compile errors, such as func(,, , -uint) which is fixed with func(,,,-(int)uint), and all the sprintf_s. Then duktape hello_world.c ran as a function in windows app store, after an hour of work.
    more..
    I'm not a js guru. But I saw the get: set: in duktape ecmascript tests, and found some terminology for searching ducktape, such as accessor, getter, setter, and found some functions but don't see them on the API interface:
    void duk_hobject_define_accessor_internal(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_hobject *getter, duk_hobject *setter, duk_small_int_t propflags)
    int duk_hobject_object_define_property(duk_context *ctx)
    duk_ret_t duk_bi_object_constructor_define_property(duk_context *ctx)
    int duk_hobject_object_define_properties(duk_context *ctx)
    I don't know if I should be trying to call them.
@svaarala
Copy link
Owner

svaarala commented Jun 8, 2014

At this moment there is no C API for creating an accessor (setter/getter) property, but it's an issue that has cropped up from time to time so it'll be added in a release or two. For now, you can write an Ecmascript helper function like:

function defineAccessor(obj, key, set, get) {
    Object.defineProperty(obj, key, {
        enumerable: true, configurable: true,
        set: set, get: get
    });
}

and then call that from C code e.g. as:

duk_eval_string(ctx, "defineAccessor");
/* push object */
/* push key */
/* push setter */
/* push getter */
duk_call(ctx, 4);

(I didn't test any of these so I hope there aren't too many typos.)

As for the rest of your scenario, perhaps we could go over it in more detail in e-mail? I'd especially like to integrate the Windows compile fixes into the master branch.

@dug9
Copy link
Author

dug9 commented Jun 9, 2014

Thanks very much Sami for the solution. (sent separate email to iki on winRT tweeks details) -Doug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants