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

NapiClass constructor cannot be subclassed #172

Closed
Jarred-Sumner opened this issue May 17, 2022 · 8 comments · Fixed by #4220 or #4306
Closed

NapiClass constructor cannot be subclassed #172

Jarred-Sumner opened this issue May 17, 2022 · 8 comments · Fixed by #4220 or #4306
Labels
bug Something isn't working napi Compatibility with the native layer of Node.js

Comments

@Jarred-Sumner
Copy link
Collaborator

Filing so I remember to fix this

This currently impacts @resvg/resvg-js

module.exports.Resvg = class Resvg extends _Resvg {
  constructor(svg, options) {
     super(svg, JSON.stringify(options))

The problem in Bun is this code:

   NapiClass* napi = jsDynamicCast<NapiClass*>(newTarget);
    if (UNLIKELY(!napi)) {
        JSC::throwVMError(globalObject, scope, JSC::createTypeError(globalObject, "NapiClass constructor called on an object that is not a NapiClass"_s));
        return JSC::JSValue::encode(JSC::jsUndefined());
    }

That !napi is being returned because newTarget is a subclass of NapiClass.

Maybe the prototype needs to be checked instead? I'm not sure.

@Jarred-Sumner
Copy link
Collaborator Author

I believe the fix here is, instead of jsDynamicCast, to check inherits and then do jsCast. I see that pattern in WebKit code a decent amount.

@Electroid Electroid added bug Something isn't working napi Compatibility with the native layer of Node.js labels Nov 1, 2022
@arturovt arturovt mentioned this issue Jul 23, 2023
@yisibl
Copy link

yisibl commented Aug 4, 2023

I'm the author of resvg-js, thank you very much for bringing this to your attention. It would be great if this could be fixed before bun 1.0 is released.

Jarred-Sumner added a commit that referenced this issue Aug 20, 2023
Jarred-Sumner added a commit that referenced this issue Aug 20, 2023
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
@yisibl
Copy link

yisibl commented Aug 24, 2023

I tested bun v0.8.0 in macOS and it still doesn't work. @Jarred-Sumner Can you help and look at it again?

image

It won't run on a Mac M1 Pro or Mac Intel CPU.

@Jarred-Sumner
Copy link
Collaborator Author

Jarred-Sumner commented Aug 24, 2023 via email

@yisibl
Copy link

yisibl commented Aug 24, 2023

I submitted a test repository for reference: https://github.com/yisibl/resvg-js-bun-test

@Jarred-Sumner Jarred-Sumner reopened this Aug 24, 2023
@Jarred-Sumner
Copy link
Collaborator Author

The cause is a transpiler bug introduced after the napi-related parts were fixed

Input:

localFileExisted = existsSync(
  join(__dirname, "resvgjs.darwin-arm64.node")
);
try {
  localFileExisted
    ? (nativeBinding = require("./resvgjs.darwin-arm64.node"))
    : (nativeBinding = require("@resvg/resvg-js-darwin-arm64"));
} catch (e) {
  loadError = e;
}

Output:

localFileExisted = existsSync(join(__dirname, "resvgjs.darwin-arm64.node"));
try {
  localFileExisted
    ? (nativeBinding = () => ({}))
    : (nativeBinding = require("/path/to/resvgjs.darwin-arm64.node"));
} catch (e) {
  loadError = e;
}

@yisibl
Copy link

yisibl commented Aug 24, 2023

Thanks for the quick find of the cause of the problem

@yisibl
Copy link

yisibl commented Aug 25, 2023

Thanks, I confirmed the fix in bun v0.8.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working napi Compatibility with the native layer of Node.js
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants