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

Allow host functions to be treated as constructors #220

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ObsidianX
Copy link

When creating new functions on the host side, it's not currently possible to create a constructor that can be called with new from within the VM. The only thing that needs to change to enable this is the function type used when creating the function with JS_NewCFunctionMagic.

Example:

Host:

vm.newFunction('Cat', () => {
  const cat = vm.newObject();
  vm.setProp(cat, 'breed', 'calico');
  return cat;
});

Script:

const cat = new Cat();
console.log(cat.breed);
// "calico"

Without this change the error "not a constructor" is thrown instead.

Creating synthetic constructors is also supported in the qjs REPL:

qjs > function Cat() { return { breed: 'calico' }; }
qjs > const cat = new Cat()
qjs > console.log(cat.breed)
calico

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

Successfully merging this pull request may close these issues.

1 participant