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

Cannot register classes and function into an object instead of global #12

Open
mean-ui-thread opened this issue Feb 19, 2018 · 2 comments

Comments

@mean-ui-thread
Copy link

To better explain what the issue is, I just wanted to be able to do something like this:

    duk_push_object(ctx);

    dukglue_register_constructor<Dog>(ctx, "Dog");
    dukglue_register_constructor<Dog>(ctx, "Cat");

    duk_put_global_string(ctx, "Animal");

so that they can be used like so:

var dog = new Animal.Dog();
var cat = new Animal.Cat();

However, Dog and Cat always appear in the global scope instead of in the Animal scope.

@mean-ui-thread
Copy link
Author

I managed to get this to work by modifying a copy of dukglue code locally like so:

in register_class.h:

    // set name = constructor_func
    duk_put_global_string(ctx, name);

into

    // set name = constructor_func
    duk_put_prop_string(ctx, -2, name);

However, this breaks the ability to put classes in the global scope. I hope there is a better way to achieve my goal.

@justinbowes
Copy link

I think you could use this technique for defining an explicit global object:
http://wiki.duktape.org/HowtoGlobalObjectReference.html

...then use your register_class modification, but specify "global" for the target when that is what you want.

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