Skip to content
Draft
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.TopLevel;
import org.mozilla.javascript.Undefined;
import org.mozilla.javascript.VarScope;
import org.mozilla.javascript.annotations.*;
import org.openjdk.jmh.annotations.*;

Expand Down Expand Up @@ -56,7 +58,7 @@ public void close() {
}

Context cx;
Scriptable scope;
TopLevel scope;
Script testScript;
}

Expand All @@ -74,7 +76,7 @@ public void init()

@Benchmark
public Object annotatedClassMethods(AnnotatedClassState state) {
return state.testScript.exec(state.cx, state.scope, state.scope);
return state.testScript.exec(state.cx, state.scope, state.scope.getGlobalThis());
}

public static class AnnotatedClass extends ScriptableObject {
Expand Down Expand Up @@ -138,14 +140,14 @@ public void init()

@Benchmark
public Object idClassMethods(IdClassState state) {
return state.testScript.exec(state.cx, state.scope, state.scope);
return state.testScript.exec(state.cx, state.scope, state.scope.getGlobalThis());
}

public static class IdClass extends IdScriptableObject {

private static final String TAG = "IdClass";

public static void init(Scriptable scope) {
public static void init(TopLevel scope) {
IdClass idc = new IdClass();
idc.exportAsJSClass(MAX_ID, scope, false);
}
Expand Down Expand Up @@ -217,11 +219,7 @@ protected void initPrototypeId(int id) {

@Override
public Object execIdCall(
IdFunctionObject f,
Context cx,
Scriptable scope,
Scriptable thisObj,
Object[] args) {
IdFunctionObject f, Context cx, VarScope scope, Scriptable thisObj, Object[] args) {
if (!f.hasTag(TAG)) {
return super.execIdCall(f, cx, scope, thisObj, args);
}
Expand Down Expand Up @@ -331,18 +329,17 @@ public void init()

@Benchmark
public Object dumbLambdaClassMethods(DumbLambdaState state) {
return state.testScript.exec(state.cx, state.scope, state.scope);
return state.testScript.exec(state.cx, state.scope, state.scope.getGlobalThis());
}

private static class DumbLambdaClass extends ScriptableObject {

private static Object noop(
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
private static Object noop(Context cx, VarScope scope, Scriptable thisObj, Object[] args) {
return Undefined.instance;
}

private static Object setValue(
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Context cx, VarScope scope, Scriptable thisObj, Object[] args) {
if (args.length < 1) {
throw ScriptRuntime.throwError(cx, scope, "Not enough args");
}
Expand All @@ -353,19 +350,19 @@ private static Object setValue(
}

private static Object getValue(
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Context cx, VarScope scope, Scriptable thisObj, Object[] args) {
DumbLambdaClass self =
LambdaConstructor.convertThisObject(thisObj, DumbLambdaClass.class);
return self.value;
}

public static void init(Scriptable scope) {
public static void init(VarScope scope) {
LambdaConstructor cons =
new LambdaConstructor(
scope,
"DumbLambdaClass",
0,
(Context cx, Scriptable s, Object[] args) -> new DumbLambdaClass());
(Context cx, VarScope s, Object[] args) -> new DumbLambdaClass());
cons.definePrototypeMethod(scope, "one", 0, DumbLambdaClass::noop);
cons.definePrototypeMethod(scope, "two", 0, DumbLambdaClass::noop);
cons.definePrototypeMethod(scope, "three", 0, DumbLambdaClass::noop);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.TopLevel;
import org.openjdk.jmh.annotations.*;

@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class GeneratorBenchmark {
@State(Scope.Thread)
public static class GeneratorState {
Context cx;
Scriptable scope;
TopLevel scope;

Function nativeGenerator;
Function transpiledGenerator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.TopLevel;
import org.openjdk.jmh.annotations.*;

@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class MathBenchmark {
@State(Scope.Thread)
public static class MathState {
Context cx;
Scriptable scope;
TopLevel scope;

Function addConstantInts;
Function addIntAndConstant;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.TopLevel;
import org.mozilla.javascript.VarScope;
import org.mozilla.javascript.tools.shell.Global;
import org.openjdk.jmh.annotations.*;

Expand All @@ -20,7 +22,7 @@ public class ObjectBenchmark {
// "count" should match "@OperationsPerInvocation" annotations
static final int count = 1000;

static void runCode(Context cx, Scriptable scope, String fileName) throws IOException {
static void runCode(Context cx, VarScope scope, String fileName) throws IOException {
try (FileReader rdr = new FileReader(fileName)) {
cx.evaluateReader(scope, rdr, "test.js", 1, null);
}
Expand All @@ -29,7 +31,7 @@ static void runCode(Context cx, Scriptable scope, String fileName) throws IOExce
@State(Scope.Thread)
public static class FieldTestState {
Context cx;
Scriptable scope;
TopLevel scope;
Scriptable strings;
Scriptable ints;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.TopLevel;
import org.openjdk.jmh.annotations.*;

@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class PropertyBenchmark {
@State(Scope.Thread)
public static class PropertyState {
Context cx;
Scriptable scope;
TopLevel scope;

Function create;
Function createFieldByField;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import java.util.concurrent.TimeUnit;
import org.mozilla.javascript.Callable;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.TopLevel;
import org.openjdk.jmh.annotations.*;

@OutputTimeUnit(TimeUnit.MICROSECONDS)
Expand All @@ -16,7 +16,7 @@ public class RegexpAtomicQuantifierBenchmark {
@State(Scope.Thread)
public static class RegExpState {
Context cx;
Scriptable scope;
TopLevel scope;
Callable testFunc;

@Setup(Level.Trial)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.concurrent.TimeUnit;
import org.mozilla.javascript.EmbeddedSlotMap;
import org.mozilla.javascript.HashSlotMap;
import org.mozilla.javascript.PropHolder;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.Slot;
import org.mozilla.javascript.SlotMap;
import org.openjdk.jmh.annotations.*;
Expand All @@ -15,9 +17,9 @@ public class SlotMapBenchmark {

@State(Scope.Thread)
public static class EmbeddedState {
final EmbeddedSlotMap emptyMap = new EmbeddedSlotMap();
final EmbeddedSlotMap size10Map = new EmbeddedSlotMap();
final EmbeddedSlotMap size100Map = new EmbeddedSlotMap();
final EmbeddedSlotMap<Scriptable> emptyMap = new EmbeddedSlotMap<>();
final EmbeddedSlotMap<Scriptable> size10Map = new EmbeddedSlotMap<>();
final EmbeddedSlotMap<Scriptable> size100Map = new EmbeddedSlotMap<>();
final String[] randomKeys = new String[100];
String size100LastKey;
String size10LastKey;
Expand All @@ -42,7 +44,7 @@ public void create() {
@Benchmark
@OperationsPerInvocation(100)
public Object embeddedInsert1Key(EmbeddedState state) {
Slot newSlot = null;
Slot<Scriptable> newSlot = null;
for (int i = 0; i < 100; i++) {
newSlot = state.emptyMap.modify(null, state.randomKeys[i], 0, 0);
}
Expand All @@ -55,7 +57,7 @@ public Object embeddedInsert1Key(EmbeddedState state) {
@Benchmark
@OperationsPerInvocation(100)
public Object embeddedQueryKey10Entries(EmbeddedState state) {
Slot slot = null;
Slot<Scriptable> slot = null;
for (int i = 0; i < 100; i++) {
slot = state.size10Map.query(state.size10LastKey, 0);
}
Expand All @@ -68,7 +70,7 @@ public Object embeddedQueryKey10Entries(EmbeddedState state) {
@Benchmark
@OperationsPerInvocation(100)
public Object embeddedQueryKey100Entries(EmbeddedState state) {
Slot slot = null;
Slot<Scriptable> slot = null;
for (int i = 0; i < 100; i++) {
slot = state.size100Map.query(state.size100LastKey, 0);
}
Expand All @@ -80,9 +82,9 @@ public Object embeddedQueryKey100Entries(EmbeddedState state) {

@State(Scope.Thread)
public static class HashState {
final HashSlotMap emptyMap = new HashSlotMap();
final HashSlotMap size10Map = new HashSlotMap();
final HashSlotMap size100Map = new HashSlotMap();
final HashSlotMap<Scriptable> emptyMap = new HashSlotMap<>();
final HashSlotMap<Scriptable> size10Map = new HashSlotMap<>();
final HashSlotMap<Scriptable> size100Map = new HashSlotMap<>();
final String[] randomKeys = new String[100];
String size100LastKey;
String size10LastKey;
Expand All @@ -107,7 +109,7 @@ public void create() {
@Benchmark
@OperationsPerInvocation(100)
public Object hashInsert1Key(HashState state) {
Slot newSlot = null;
Slot<Scriptable> newSlot = null;
for (int i = 0; i < 100; i++) {
newSlot = state.emptyMap.modify(null, state.randomKeys[i], 0, 0);
}
Expand All @@ -120,7 +122,7 @@ public Object hashInsert1Key(HashState state) {
@Benchmark
@OperationsPerInvocation(100)
public Object hashQueryKey10Entries(HashState state) {
Slot slot = null;
Slot<Scriptable> slot = null;
for (int i = 0; i < 100; i++) {
slot = state.size10Map.query(state.size10LastKey, 0);
}
Expand All @@ -133,7 +135,7 @@ public Object hashQueryKey10Entries(HashState state) {
@Benchmark
@OperationsPerInvocation(100)
public Object hashQueryKey100Entries(HashState state) {
Slot slot = null;
Slot<Scriptable> slot = null;
for (int i = 0; i < 100; i++) {
slot = state.size100Map.query(state.size100LastKey, 0);
}
Expand All @@ -154,9 +156,9 @@ private static String makeRandomString() {
}

/** Insert a random key and value into the map */
private static String insertRandomEntry(SlotMap map) {
private static <T extends PropHolder<T>> String insertRandomEntry(SlotMap<T> map) {
String key = makeRandomString();
Slot slot = map.modify(null, key, 0, 0);
Slot<T> slot = map.modify(null, key, 0, 0);
slot.setValue(key, null, null);
return key;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.concurrent.TimeUnit;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.TopLevel;
import org.openjdk.jmh.annotations.*;

public class SunSpiderBenchmark {
Expand All @@ -14,7 +14,7 @@ public class SunSpiderBenchmark {
@OutputTimeUnit(TimeUnit.MICROSECONDS)
abstract static class AbstractState {
Context cx;
Scriptable scope;
TopLevel scope;
Script script;
String fileName;

Expand Down Expand Up @@ -44,7 +44,7 @@ public void tearDown() {
}

Object run() {
return script.exec(cx, scope, scope);
return script.exec(cx, scope, scope.getGlobalThis());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.TopLevel;
import org.openjdk.jmh.annotations.*;

@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class ThrowBenchmark {
@State(Scope.Thread)
public static class GeneratorState {
Context cx;
Scriptable scope;
TopLevel scope;

Function shallowThrow;
Function mediumThrow;
Expand Down
Loading
Loading