Skip to content

Commit

Permalink
Use getObjectElem(SymbolKey) lookup
Browse files Browse the repository at this point in the history
Faster and safer (it works even if the code does crazy things such as
`delete Symbol`)
  • Loading branch information
andreabergia committed Nov 25, 2024
1 parent 5067e6a commit 9d2d4ae
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions rhino/src/main/java/org/mozilla/javascript/NativeString.java
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,9 @@ else if (Normalizer.Form.NFC.name().equals(formStr))
}
}

Object matcher = getPropSymbolMatchAll(cx, scope, regexp);
Object matcher =
ScriptRuntime.getObjectElem(
regexp, SymbolKey.MATCH_ALL, cx, scope);
// If method is not undefined, it should be a Callable
if (matcher != null && !Undefined.isUndefined(matcher)) {
if (!(matcher instanceof Callable)) {
Expand All @@ -907,7 +909,8 @@ else if (Normalizer.Form.NFC.name().equals(formStr))
Object compiledRegExp = regExpProxy.compileRegExp(cx, regexpToString, "g");
Scriptable rx = regExpProxy.wrapRegExp(cx, scope, compiledRegExp);

Object method = getPropSymbolMatchAll(cx, scope, rx);
Object method =
ScriptRuntime.getObjectElem(rx, SymbolKey.MATCH_ALL, cx, scope);
if (!(method instanceof Callable)) {
throw ScriptRuntime.notFunctionError(
rx, method, SymbolKey.MATCH_ALL.getName());
Expand All @@ -920,13 +923,6 @@ else if (Normalizer.Form.NFC.name().equals(formStr))
}
}

private static Object getPropSymbolMatchAll(Context cx, Scriptable scope, Object object) {
// Technically this _could_ fail if someone removed Symbol... but that's really an edge case
Scriptable symbol = (Scriptable) ScriptRuntime.name(cx, scope, "Symbol");
Symbol symbolMatchAll = (Symbol) ScriptRuntime.getObjectProp(symbol, "matchAll", cx, scope);
return ScriptRuntime.getObjectElem(object, symbolMatchAll, cx, scope);
}

private static NativeString realThis(Scriptable thisObj, IdFunctionObject f) {
return ensureType(thisObj, NativeString.class, f);
}
Expand Down

0 comments on commit 9d2d4ae

Please sign in to comment.