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

Example of C class extending JS class #333

Open
Sytten opened this issue Jul 19, 2024 · 7 comments
Open

Example of C class extending JS class #333

Sytten opened this issue Jul 19, 2024 · 7 comments

Comments

@Sytten
Copy link

Sytten commented Jul 19, 2024

Hi!

Does someone have an example of a C class extending a JS class?
Essentially the inverse of the Point example.

I am trying to re-create the node Buffer class in C and I want to extend the ArrayBuffer class.

Thanks

@saghul
Copy link
Contributor

saghul commented Jul 19, 2024

IIRC the Buffer class extends Uint8Array.

Anyway. I think you can get the prototype with JS_GetPrototype and then add your custom methods.

@Sytten
Copy link
Author

Sytten commented Jul 19, 2024

Yes sorry Uint8Array.

That would work for the prototype, I probably also need something for the constructor to call the parent one.

@saghul
Copy link
Contributor

saghul commented Jul 22, 2024

Good point. I'm not sure QuickJS provider the necessary APIs to do that... @chqrlie ?

@Sytten
Copy link
Author

Sytten commented Jul 22, 2024

I tried to use JS_GetClassProto with the class id of Uint8Array (12 at the moment), but it always returns an empty prototype for some reason. JS_GetPrototype on the globals does return something though.
I was thinking of calling the prototype.constructor function if that exists? I have not tested it yet.

In reality I am writing that for the rust library rquickjs so I need to massage it a bit more but I didn't want to add complexity to the example.

@saghul
Copy link
Contributor

saghul commented Jul 22, 2024

Calling constructor on the prototype might work indeed!

@chqrlie
Copy link
Collaborator

chqrlie commented Jul 22, 2024

@saghul Sorry, cannot comment right now, travelling 1200 km from the North pole with very little Internet coverage :)

@Sytten
Copy link
Author

Sytten commented Nov 5, 2024

@chqrlie If you have time, still looking for guidance on it

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

3 participants