-
Notifications
You must be signed in to change notification settings - Fork 18
Prototope, Xcode, and Protocaster all crash when you forget “new” in your script #25
Comments
Investigated a bit – looks like the constructor functions that get created for the This is an actual crash (not a thrown exception) in JavaScriptCore, couldn’t figure out a workaround :( We can either live with it or stop using the constructor and expose a |
Yikes. I've got no bright ideas here, other than e.g. preprocessing the JS to look for this kind of mistake, which is probably overboard. @jbrennan? |
It's probably not a bad idea to do some sort of linting before running. If we're looking to support JavaScript as a language it shouldn't look too different from regular JavaScript that you see in the browser/node. For instance, I was surprised that there was no |
How do we define the constructors? I don't know if this is possible because I haven't really looked at the bridging code, but you can detect in a constructor if it's been invoked correctly by checking |
Constructor objects are defined by JavaScriptCore automatically based on the exposed Sent from my iPhone
|
Just seeing this in passing via email so I haven't looked at how the code is set up, but if you can run something along these lines before the user code, perhaps it will help: function wrapNativeClass(OriginalConstructor) {
function Constructor() {
if (!(this instanceof Constructor)) {
throw new Error("Classes should be instantiated with `new`");
}
OriginalConstructor.apply(this, arguments);
}
Constructor.prototype = OriginalConstructor.prototype;
return Constructor;
}
TextLayer = wrapNativeClass(TextLayer);
|
For Khan#25: This shows how we might wrap the constructors so that they throw when called without `new`. In this example, I've hardcoded `TextLayer` as the only wrapped type, but we'd presumably want to wrap every instantiable type. When you call a type without `new`, you now get a protonope, as you might expect. (It always says line 5 though since that's where the `new Error()` call is in the wrap script -- not sure how to fix that easily.)
For Khan#25: This shows how we might wrap the constructors so that they throw when called without `new`. In this example, I've hardcoded `TextLayer` as the only wrapped type, but we'd presumably want to wrap every instantiable type. When you call a type without `new`, you now get a protonope, as you might expect. (It always says line 5 though since that's where the `new Error()` call is in the wrap script -- not sure how to fix that easily.)
This one was fun. I had the following line in my script,
And I couldn’t figure out what was wrong with it. Turns out I forgot
new
. Anyway, when Prototope hit that line, it crashed, and sometimes this caused Xcode to crash (betas!). Obviously the script is wrong but for some reason Prototope didn’t catch the exception...not quite sure why yet.The text was updated successfully, but these errors were encountered: