Skip to content

Commit

Permalink
8265691: Some Object constructor methods aren't ES6 compliant (#14)
Browse files Browse the repository at this point in the history
* Have a Global.isES6 method

* Use Parser.isES6()

* Fix freeze, isFrozen, seal, isSealed, preventExtensions, isExtensible, getOwnPropertyNames, getOwnPropertySymbols for ES6
  • Loading branch information
szegedi committed Apr 23, 2021
1 parent 60c0e24 commit b5e29d4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ private static PropertyMap checkAndGetMap(final Context context) {
public Global(final Context context) {
super(checkAndGetMap(context));
this.context = context;
this.lexicalScope = context.getEnv()._es6 ? new LexicalScope(this) : null;
this.lexicalScope = isES6() ? new LexicalScope(this) : null;
}

/**
Expand Down Expand Up @@ -2392,12 +2392,16 @@ public static void checkObjectCoercible(final Object obj) {
}
}

public boolean isES6() {
return context.getEnv()._es6;
}

/**
* Return the ES6 global scope for lexically declared bindings.
* @return the ES6 lexical global scope.
*/
public final ScriptObject getLexicalScope() {
assert context.getEnv()._es6;
assert isES6();
return lexicalScope;
}

Expand All @@ -2408,7 +2412,7 @@ public void addBoundProperties(final ScriptObject source, final org.openjdk.nash
PropertyMap lexicalMap = null;
boolean hasLexicalDefinitions = false;

if (context.getEnv()._es6) {
if (isES6()) {
lexScope = (LexicalScope) getLexicalScope();
lexicalMap = lexScope.getMap();

Expand Down Expand Up @@ -2470,7 +2474,7 @@ public GuardedInvocation findGetMethod(final CallSiteDescriptor desc, final Link
// We therefore check if the invocation does already have a switchpoint and the property is non-inherited,
// assuming this only applies to global constants. If other non-inherited properties will
// start using switchpoints some time in the future we'll have to revisit this.
if (isScope && context.getEnv()._es6 && (invocation.getSwitchPoints() == null || !hasOwnProperty(name))) {
if (isScope && isES6() && (invocation.getSwitchPoints() == null || !hasOwnProperty(name))) {
return invocation.addSwitchPoint(getLexicalScopeSwitchPoint());
}

Expand Down Expand Up @@ -2501,7 +2505,7 @@ public GuardedInvocation findSetMethod(final CallSiteDescriptor desc, final Link

final GuardedInvocation invocation = super.findSetMethod(desc, request);

if (isScope && context.getEnv()._es6) {
if (isScope && isES6()) {
return invocation.addSwitchPoint(getLexicalScopeSwitchPoint());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,16 @@ public static ScriptObject getOwnPropertyNames(final Object self, final Object o
return new NativeArray(((ScriptObject)obj).getOwnKeys(true));
} else if (obj instanceof ScriptObjectMirror) {
return new NativeArray(((ScriptObjectMirror)obj).getOwnKeys(true));
} else {
throw notAnObject(obj);
}
final var global = Global.instance();
if (global.isES6()) {
final var obj2 = JSType.toScriptObject(global, obj);
if (obj2 instanceof ScriptObject) {
return new NativeArray(((ScriptObject)obj2).getOwnKeys(true));
}
return new NativeArray();
}
throw notAnObject(obj);
}

/**
Expand All @@ -261,12 +268,12 @@ public static ScriptObject getOwnPropertyNames(final Object self, final Object o
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static ScriptObject getOwnPropertySymbols(final Object self, final Object obj) {
if (obj instanceof ScriptObject) {
return new NativeArray(((ScriptObject)obj).getOwnSymbols(true));
} else {
// TODO: we don't support this on ScriptObjectMirror objects yet
throw notAnObject(obj);
final var obj2 = JSType.toScriptObject(obj);
if (obj2 instanceof ScriptObject) {
return new NativeArray(((ScriptObject)obj2).getOwnSymbols(true));
}
// TODO: we don't support this on ScriptObjectMirror objects yet
return new NativeArray();
}

/**
Expand Down Expand Up @@ -346,6 +353,8 @@ public static Object seal(final Object self, final Object obj) {
return ((ScriptObject)obj).seal();
} else if (obj instanceof ScriptObjectMirror) {
return ((ScriptObjectMirror)obj).seal();
} else if (isES6()) {
return obj;
} else {
throw notAnObject(obj);
}
Expand All @@ -365,11 +374,17 @@ public static Object freeze(final Object self, final Object obj) {
return ((ScriptObject)obj).freeze();
} else if (obj instanceof ScriptObjectMirror) {
return ((ScriptObjectMirror)obj).freeze();
} else if (isES6()) {
return obj;
} else {
throw notAnObject(obj);
}
}

private static boolean isES6() {
return Global.instance().isES6();
}

/**
* ECMA 15.2.3.10 Object.preventExtensions ( O )
*
Expand All @@ -383,6 +398,8 @@ public static Object preventExtensions(final Object self, final Object obj) {
return ((ScriptObject)obj).preventExtensions();
} else if (obj instanceof ScriptObjectMirror) {
return ((ScriptObjectMirror)obj).preventExtensions();
} else if (isES6()) {
return obj;
} else {
throw notAnObject(obj);
}
Expand All @@ -401,6 +418,8 @@ public static boolean isSealed(final Object self, final Object obj) {
return ((ScriptObject)obj).isSealed();
} else if (obj instanceof ScriptObjectMirror) {
return ((ScriptObjectMirror)obj).isSealed();
} else if (isES6()) {
return true;
} else {
throw notAnObject(obj);
}
Expand All @@ -419,6 +438,8 @@ public static boolean isFrozen(final Object self, final Object obj) {
return ((ScriptObject)obj).isFrozen();
} else if (obj instanceof ScriptObjectMirror) {
return ((ScriptObjectMirror)obj).isFrozen();
} else if (isES6()) {
return true;
} else {
throw notAnObject(obj);
}
Expand All @@ -437,6 +458,8 @@ public static boolean isExtensible(final Object self, final Object obj) {
return ((ScriptObject)obj).isExtensible();
} else if (obj instanceof ScriptObjectMirror) {
return ((ScriptObjectMirror)obj).isExtensible();
} else if (isES6()) {
return false;
} else {
throw notAnObject(obj);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public FunctionNode parse(final String scriptName, final int startPos, final int

try {
stream = new TokenStream();
lexer = new Lexer(source, startPos, len, stream, scripting && !env._no_syntax_extensions, env._es6, reparsedFunction != null);
lexer = new Lexer(source, startPos, len, stream, scripting && !env._no_syntax_extensions, isES6(), reparsedFunction != null);
lexer.line = lexer.pendingLine = lineOffset + 1;
line = lineOffset;

Expand Down Expand Up @@ -349,7 +349,7 @@ public FunctionNode parse(final String scriptName, final int startPos, final int
public FunctionNode parseModule(final String moduleName, final int startPos, final int len) {
try {
stream = new TokenStream();
lexer = new Lexer(source, startPos, len, stream, scripting && !env._no_syntax_extensions, env._es6, reparsedFunction != null);
lexer = new Lexer(source, startPos, len, stream, scripting && !env._no_syntax_extensions, isES6(), reparsedFunction != null);
lexer.line = lexer.pendingLine = lineOffset + 1;
line = lineOffset;

Expand Down Expand Up @@ -383,7 +383,7 @@ public FunctionNode parseModule(final String moduleName) {
public void parseFormalParameterList() {
try {
stream = new TokenStream();
lexer = new Lexer(source, stream, scripting && !env._no_syntax_extensions, env._es6);
lexer = new Lexer(source, stream, scripting && !env._no_syntax_extensions, isES6());

scanFirstToken();

Expand All @@ -403,7 +403,7 @@ public void parseFormalParameterList() {
public void parseFunctionBody() {
try {
stream = new TokenStream();
lexer = new Lexer(source, stream, scripting && !env._no_syntax_extensions, env._es6);
lexer = new Lexer(source, stream, scripting && !env._no_syntax_extensions, isES6());
final int functionLine = line;

scanFirstToken();
Expand Down Expand Up @@ -664,7 +664,7 @@ private void detectSpecialProperty(final IdentNode ident) {
}

private boolean useBlockScope() {
return env._es6;
return isES6();
}

private boolean isES6() {
Expand Down Expand Up @@ -1984,7 +1984,7 @@ private void forStatement() {
break;

case IDENT:
if (env._es6 && "of".equals(getValue())) {
if (isES6() && "of".equals(getValue())) {
isForOf = true;
// fall through
} else {
Expand Down Expand Up @@ -4260,7 +4260,7 @@ private boolean skipFunctionBody(final ParserContextFunctionNode functionNode) {
}

stream.reset();
lexer = parserState.createLexer(source, lexer, stream, scripting && !env._no_syntax_extensions, env._es6);
lexer = parserState.createLexer(source, lexer, stream, scripting && !env._no_syntax_extensions, isES6());
line = parserState.line;
linePosition = parserState.linePosition;
// Doesn't really matter, but it's safe to treat it as if there were a semicolon before
Expand Down

0 comments on commit b5e29d4

Please sign in to comment.