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

get property by index #45

Open
rkd77 opened this issue Sep 4, 2021 · 3 comments
Open

get property by index #45

rkd77 opened this issue Sep 4, 2021 · 3 comments

Comments

@rkd77
Copy link

rkd77 commented Sep 4, 2021

In mozjs52 JSClass had method getProperty. Newer versions have not. Could you add an example how to properly access elements by index?
For example getElementsByTagName() returns array. a = document.getElementsByTagName("H1")[5];

@evilpie
Copy link
Contributor

evilpie commented Sep 6, 2021

Do you mean something like JS_GetElement that takes an uint32_t index?

@rkd77
Copy link
Author

rkd77 commented Sep 6, 2021

I don't know. getElementsByTagName(...) returns some object. and now I want to code somehow class for this object, so it can be accessed by index. In mozjs52 I had:

static bool
htmlCollection_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
{
   jsid id = hid.get();
...
   if (JSID_IS_INT(id)) {
                JS::RootedValue r_idval(ctx, idval);
                JS_IdToValue(ctx, id, &r_idval);
                int index = r_idval.toInt32();
                return htmlCollection_item2(ctx, hobj, index, hvp);
        }
...
}

JSClassOps htmlCollection_ops = {
  JS_PropertyStub, nullptr,
  htmlCollection_get_property, JS_StrictPropertyStub,
  ...
};

but AFAIK, in mozjs78 there is no getProperty in JSClassOps.
So question is how to achieve similar result with mozjs78.

@ptomato
Copy link
Collaborator

ptomato commented Sep 15, 2021

Right, getProperty and setProperty were removed from JSClassOps. You will either have to use lazy property resolution (example https://github.com/mozilla-spidermonkey/spidermonkey-embedding-examples/blob/esr78/examples/resolve.cpp) or pre-define the properties.

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

No branches or pull requests

3 participants