Skip to content

Commit

Permalink
Complete work on new Symbol implementation, replacing the old
Browse files Browse the repository at this point in the history
bare-bones support.

As per ECMAscript, object properties may now be keyed by symbols just
like they can be keyed by strings and integers. This required extensions
to a number of internal classes including ScriptableObject.

Also, there is now an internal SymbolKey mechanism so that we can
easily and efficiently use Symbols as keys to built-in objects.
  • Loading branch information
Gregory Brail authored and gbrail committed Aug 3, 2017
1 parent f0358ee commit 6f0f1f6
Show file tree
Hide file tree
Showing 21 changed files with 1,794 additions and 195 deletions.
11 changes: 7 additions & 4 deletions src/org/mozilla/javascript/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Arguments(NativeCall activation)
callerObj = NOT_FOUND;
}

defineProperty(NativeSymbol.ITERATOR_PROPERTY, iteratorMethod, ScriptableObject.DONTENUM);
defineProperty(SymbolKey.ITERATOR, iteratorMethod, ScriptableObject.DONTENUM);
}

@Override
Expand Down Expand Up @@ -284,9 +284,9 @@ protected void setInstanceIdAttributes(int id, int attr)
}

@Override
Object[] getIds(boolean getAll)
Object[] getIds(boolean getNonEnumerable, boolean getSymbols)
{
Object[] ids = super.getIds(getAll);
Object[] ids = super.getIds(getNonEnumerable, getSymbols);
if (args.length != 0) {
boolean[] present = new boolean[args.length];
int extraCount = args.length;
Expand All @@ -302,7 +302,7 @@ Object[] getIds(boolean getAll)
}
}
}
if (!getAll) { // avoid adding args which were redefined to non-enumerable
if (!getNonEnumerable) { // avoid adding args which were redefined to non-enumerable
for (int i = 0; i < present.length; i++) {
if (!present[i] && super.has(i, this)) {
present[i] = true;
Expand All @@ -329,6 +329,9 @@ Object[] getIds(boolean getAll)

@Override
protected ScriptableObject getOwnPropertyDescriptor(Context cx, Object id) {
if (id instanceof Scriptable) {
return super.getOwnPropertyDescriptor(cx, id);
}
double d = ScriptRuntime.toNumber(id);
int index = (int) d;
if (d != index) {
Expand Down
23 changes: 13 additions & 10 deletions src/org/mozilla/javascript/ES6Iterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

package org.mozilla.javascript;

import static org.mozilla.javascript.NativeSymbol.ITERATOR_PROPERTY;
import static org.mozilla.javascript.NativeSymbol.TO_STRING_TAG_PROPERTY;

public abstract class ES6Iterator extends IdScriptableObject {

static void init(ScriptableObject scope, boolean sealed, IdScriptableObject prototype, String tag) {
Expand Down Expand Up @@ -53,10 +50,10 @@ protected void initPrototypeId(int id)
initPrototypeMethod(getTag(), id, NEXT_METHOD, 0);
return;
case Id_iterator:
initPrototypeMethod(getTag(), id, ITERATOR_PROPERTY, "[Symbol.iterator]", 0);
initPrototypeMethod(getTag(), id, SymbolKey.ITERATOR, "[Symbol.iterator]", 0);
return;
case Id_toStringTag:
initPrototypeValue(Id_toStringTag, TO_STRING_TAG_PROPERTY, getClassName(), DONTENUM | READONLY);
initPrototypeValue(Id_toStringTag, SymbolKey.TO_STRING_TAG, getClassName(), DONTENUM | READONLY);
return;
default: throw new IllegalArgumentException(String.valueOf(id));
}
Expand Down Expand Up @@ -87,17 +84,23 @@ public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
}

@Override
protected int findPrototypeId(String s) {
if (s.charAt(0) == 'n') {
return Id_next;
} else if (ITERATOR_PROPERTY.equals(s)) {
protected int findPrototypeId(Symbol k) {
if (SymbolKey.ITERATOR.equals(k)) {
return Id_iterator;
} else if (TO_STRING_TAG_PROPERTY.equals(s)) {
} else if (SymbolKey.TO_STRING_TAG.equals(k)) {
return Id_toStringTag;
}
return 0;
}

@Override
protected int findPrototypeId(String s) {
if ("next".equals(s)) {
return Id_next;
}
return 0;
}

abstract protected boolean isDone(Context cx, Scriptable scope);

abstract protected Object nextValue(Context cx, Scriptable scope);
Expand Down
62 changes: 61 additions & 1 deletion src/org/mozilla/javascript/IRFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,67 @@

package org.mozilla.javascript;

import org.mozilla.javascript.ast.*;
import org.mozilla.javascript.ast.ArrayComprehension;
import org.mozilla.javascript.ast.ArrayComprehensionLoop;
import org.mozilla.javascript.ast.ArrayLiteral;
import org.mozilla.javascript.ast.Assignment;
import org.mozilla.javascript.ast.AstNode;
import org.mozilla.javascript.ast.AstRoot;
import org.mozilla.javascript.ast.Block;
import org.mozilla.javascript.ast.BreakStatement;
import org.mozilla.javascript.ast.CatchClause;
import org.mozilla.javascript.ast.ConditionalExpression;
import org.mozilla.javascript.ast.ContinueStatement;
import org.mozilla.javascript.ast.DestructuringForm;
import org.mozilla.javascript.ast.DoLoop;
import org.mozilla.javascript.ast.ElementGet;
import org.mozilla.javascript.ast.EmptyExpression;
import org.mozilla.javascript.ast.ExpressionStatement;
import org.mozilla.javascript.ast.ForInLoop;
import org.mozilla.javascript.ast.ForLoop;
import org.mozilla.javascript.ast.FunctionCall;
import org.mozilla.javascript.ast.FunctionNode;
import org.mozilla.javascript.ast.GeneratorExpression;
import org.mozilla.javascript.ast.GeneratorExpressionLoop;
import org.mozilla.javascript.ast.IfStatement;
import org.mozilla.javascript.ast.InfixExpression;
import org.mozilla.javascript.ast.Jump;
import org.mozilla.javascript.ast.Label;
import org.mozilla.javascript.ast.LabeledStatement;
import org.mozilla.javascript.ast.LetNode;
import org.mozilla.javascript.ast.Loop;
import org.mozilla.javascript.ast.Name;
import org.mozilla.javascript.ast.NewExpression;
import org.mozilla.javascript.ast.NumberLiteral;
import org.mozilla.javascript.ast.ObjectLiteral;
import org.mozilla.javascript.ast.ObjectProperty;
import org.mozilla.javascript.ast.ParenthesizedExpression;
import org.mozilla.javascript.ast.PropertyGet;
import org.mozilla.javascript.ast.RegExpLiteral;
import org.mozilla.javascript.ast.ReturnStatement;
import org.mozilla.javascript.ast.Scope;
import org.mozilla.javascript.ast.ScriptNode;
import org.mozilla.javascript.ast.StringLiteral;
import org.mozilla.javascript.ast.SwitchCase;
import org.mozilla.javascript.ast.SwitchStatement;
import org.mozilla.javascript.ast.Symbol;
import org.mozilla.javascript.ast.ThrowStatement;
import org.mozilla.javascript.ast.TryStatement;
import org.mozilla.javascript.ast.UnaryExpression;
import org.mozilla.javascript.ast.VariableDeclaration;
import org.mozilla.javascript.ast.VariableInitializer;
import org.mozilla.javascript.ast.WhileLoop;
import org.mozilla.javascript.ast.WithStatement;
import org.mozilla.javascript.ast.XmlDotQuery;
import org.mozilla.javascript.ast.XmlElemRef;
import org.mozilla.javascript.ast.XmlExpression;
import org.mozilla.javascript.ast.XmlFragment;
import org.mozilla.javascript.ast.XmlLiteral;
import org.mozilla.javascript.ast.XmlMemberGet;
import org.mozilla.javascript.ast.XmlPropRef;
import org.mozilla.javascript.ast.XmlRef;
import org.mozilla.javascript.ast.XmlString;
import org.mozilla.javascript.ast.Yield;

import java.util.List;
import java.util.ArrayList;
Expand Down
Loading

0 comments on commit 6f0f1f6

Please sign in to comment.