Skip to content

Commit

Permalink
Reflect - first hack for constructor detection (see mozilla#1376)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Sep 22, 2024
1 parent cc766ca commit e7a9317
Show file tree
Hide file tree
Showing 5 changed files with 1,015 additions and 701 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ protected ScriptableObject getOwnPropertyDescriptor(Context cx, Object id) {
Object trapResultObj = callTrap(trap, new Object[] {target, id});
if (!Undefined.isUndefined(trapResultObj)
&& !(trapResultObj instanceof Scriptable
&& !ScriptRuntime.isSymbol(trapResultObj))) {
&& !ScriptRuntime.isSymbol(trapResultObj))) {
throw ScriptRuntime.typeError(
"getOwnPropertyDescriptor trap has to return undefined or an object");
}
Expand Down
17 changes: 15 additions & 2 deletions rhino/src/main/java/org/mozilla/javascript/NativeReflect.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private static Scriptable construct(
Integer.toString(args.length));
}

if (!(args[0] instanceof Constructable)) {
if (!isConstructor(args[0])) {
throw ScriptRuntime.typeErrorById("msg.not.ctor", ScriptRuntime.typeof(args[0]));
}

Expand All @@ -151,7 +151,7 @@ private static Scriptable construct(
return ctor.construct(cx, scope, ScriptRuntime.emptyArgs);
}

if (args.length > 2 && !(args[2] instanceof Constructable)) {
if (args.length > 2 && !isConstructor(args[2])) {
throw ScriptRuntime.typeErrorById("msg.not.ctor", ScriptRuntime.typeof(args[2]));
}

Expand Down Expand Up @@ -198,6 +198,19 @@ private static Scriptable construct(
return newScriptable;
}

private static boolean isConstructor(final Object argument) {
// Hack for the moment because all Functions are Constructable
// see #1376 for more
if (argument instanceof LambdaConstructor) {
return true;
}
if (argument instanceof LambdaFunction) {
return false;
}

return argument instanceof Constructable;
}

private static Object defineProperty(
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
if (args.length < 3) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public void definePropertyTrapReturnsFalse() {

@Test
public void
definePropertyDescNotConfigurableAndTargetPropertyDescriptorConfigurableAndTrapResultIsTrue() {
definePropertyDescNotConfigurableAndTargetPropertyDescriptorConfigurableAndTrapResultIsTrue() {
String js =
"var target = {};\n"
+ "var p = new Proxy(target, {\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,11 @@ public void constructNoConstructorObject() {
public void constructNoConstructorFunction() {
String js =
"try {\n"
+ " Reflect.construct(function() {}, [], Date.now);\n"
+ " Reflect.construct(function() {}, [], Math.abs);\n"
+ "} catch(e) {\n"
+ " '' + e;\n"
+ "}";
// testString("TypeError: \"object\" is not a constructor.", js);
// found no way to check a function for constructor
testString("TypeError: \"function\" is not a constructor.", js);
}

@Test
Expand Down
Loading

0 comments on commit e7a9317

Please sign in to comment.