Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.mozilla.javascript.interpreterv2.operand;

import org.mozilla.javascript.CallFrameV2;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.interpreterv2.InstructionSimplification;
import org.mozilla.javascript.interpreterv2.KnownType;

public final class BooleanOperand extends Operand {
public static final BooleanOperand TRUE = new BooleanOperand(true);
public static final BooleanOperand FALSE = new BooleanOperand(false);

private final boolean value;

private BooleanOperand(boolean value) {
this.value = value;
}

@Override
public Boolean retrieve(Context cx, CallFrameV2 frame) {
return value;
}

@Override
public double retrieveDouble(CallFrameV2 frame) {
throw new UnsupportedOperationException();
}

@Override
public boolean isDouble(CallFrameV2 frame) {
return false;
}

public boolean getBoolean() {
return value;
}

@Override
public KnownType getKnownType(InstructionSimplification simplifier) {
return KnownType.BOOLEAN;
}

@Override
public void appendDebugString(StringBuilder sb) {
sb.append(value);
}

@Override
public boolean isValidJumpTableKey() {
return true;
}

@Override
public boolean coerceToBoolean(Context cx, CallFrameV2 frame) {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.mozilla.javascript.interpreterv2.operand;

import org.mozilla.javascript.CallFrameV2;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.interpreterv2.InstructionSimplification;
import org.mozilla.javascript.interpreterv2.KnownType;

public final class DoubleOperand extends Operand {

private final double value;

public DoubleOperand(double value) {
this.value = value;
}

@Override
public Double retrieve(Context cx, CallFrameV2 frame) {
return value;
}

@Override
public double retrieveDouble(CallFrameV2 frame) {
return value;
}

@Override
public boolean isDouble(CallFrameV2 frame) {
return true;
}

@Override
public KnownType getKnownType(InstructionSimplification simplifier) {
return KnownType.NUMBER;
}

@Override
public void appendDebugString(StringBuilder sb) {
sb.append(value);
}

@Override
public boolean isValidJumpTableKey() {
return true;
}

@Override
public boolean coerceToBoolean(Context cx, CallFrameV2 frame) {
return !Double.isNaN(value) && value != 0.0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.mozilla.javascript.interpreterv2.operand;

import org.mozilla.javascript.CallFrameV2;
import org.mozilla.javascript.Context;

public final class GetVarOperand extends Operand {
private final int index;

private static final GetVarOperand[] CACHE = new GetVarOperand[16];

static {
for (int i = 0; i < CACHE.length; i++) {
CACHE[i] = new GetVarOperand(i);
}
}

public static GetVarOperand createOperand(int index) {
if (index >= 0 && index < CACHE.length) {
return CACHE[index];
}
return new GetVarOperand(index);
}

private GetVarOperand(int index) {
this.index = index;
}

@Override
public Object retrieve(Context cx, CallFrameV2 frame) {
if (!frame.useActivation) {
return frame.getVar(index);
} else {
String name = frame.fnOrScript.getDescriptor().getParamOrVarName(index);
return frame.scope.get(name, frame.scope);
}
}

@Override
public Object retrieveAndWrap(Context cx, CallFrameV2 frame) {
if (!frame.useActivation) {
return frame.getVarAndWrap(index);
} else {
String name = frame.fnOrScript.getDescriptor().getParamOrVarName(index);
Object value = frame.scope.get(name, frame.scope);
return value != null ? value : frame.scope;
}
}

@Override
public double retrieveDouble(CallFrameV2 frame) {
if (!frame.useActivation) {
return frame.getVarDouble(index);
} else {
// When using activation, isDouble() returns false, so this should never be called
throw new IllegalStateException(
"retrieveDouble should not be called when using activation frames");
}
}

@Override
public boolean isDouble(CallFrameV2 frame) {
if (!frame.useActivation) {
return frame.isVarDouble(index);
} else {
// When using activation, variables are always objects (not primitives)
return false;
}
}

@Override
public Object viewValue(Context cx, CallFrameV2 frame, int offset) {
if (!frame.useActivation) {
return frame.getVarAndWrap(index);
} else {
String name = frame.fnOrScript.getDescriptor().getParamOrVarName(offset);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other methods here use "index" as the parameter not "offset". Is this right? It confused by AI...

Object value = frame.scope.get(name, frame.scope);
return value != null ? value : frame.scope;
}
}

@Override
public void appendDebugString(StringBuilder sb) {
sb.append("var.").append(index);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.mozilla.javascript.interpreterv2.operand;

import org.mozilla.javascript.CallFrameV2;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.interpreterv2.InstructionSimplification;
import org.mozilla.javascript.interpreterv2.KnownType;

public final class IntOperand extends Operand {

private final int value;

public IntOperand(int value) {
this.value = value;
}

@Override
public Integer retrieve(Context cx, CallFrameV2 frame) {
return value;
}

@Override
public double retrieveDouble(CallFrameV2 frame) {
return value;
}

@Override
public boolean isDouble(CallFrameV2 frame) {
return value == -1.0;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what this is supposed to do. Is an int ever a double?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this either. Yes, a double value (not a Double) can be compared to an int and will cause type promotion of the int side, but I'm not sure what it's intended to achieve here. @camnwalter, do you know what was intended? If you can't remember I'll change this to false.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is from something we check internally in our fork (for reasons I don't know), and when making interpreter v2, we wanted to copy the existing interpreter as closely as possible. I'm fairly sure we don't use it anywhere in this repo, so it should be fine to change it.

}

@Override
public KnownType getKnownType(InstructionSimplification simplifier) {
return KnownType.NUMBER;
}

@Override
public void appendDebugString(StringBuilder sb) {
sb.append(value);
}

@Override
public boolean isValidJumpTableKey() {
return true;
}

@Override
public boolean coerceToBoolean(Context cx, CallFrameV2 frame) {
return value != 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.mozilla.javascript.interpreterv2.operand;

import org.mozilla.javascript.CallFrameV2;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptRuntime;

public final class NegativeZeroOperand extends Operand {
public static final NegativeZeroOperand instance = new NegativeZeroOperand();

private NegativeZeroOperand() {}

@Override
public Double retrieve(Context cx, CallFrameV2 frame) {
return ScriptRuntime.negativeZeroObj;
}

@Override
public double retrieveDouble(CallFrameV2 frame) {
return ScriptRuntime.negativeZeroObj;
}

@Override
public boolean isDouble(CallFrameV2 frame) {
return true;
}

@Override
public void appendDebugString(StringBuilder sb) {
sb.append("-0");
}

@Override
public boolean isValidJumpTableKey() {
return true;
}

@Override
public boolean coerceToBoolean(Context cx, CallFrameV2 frame) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.mozilla.javascript.interpreterv2.operand;

import org.mozilla.javascript.CallFrameV2;
import org.mozilla.javascript.Context;

public final class NullOperand extends Operand {
public static final NullOperand instance = new NullOperand();

private NullOperand() {}

@Override
public Object retrieve(Context cx, CallFrameV2 frame) {
return null;
}

@Override
public double retrieveDouble(CallFrameV2 frame) {
throw new UnsupportedOperationException();
}

@Override
public boolean isDouble(CallFrameV2 frame) {
return false;
}

@Override
public void appendDebugString(StringBuilder sb) {
sb.append("null");
}

@Override
public boolean isValidJumpTableKey() {
// Null cannot be a key on the jump table
return false;
}

@Override
public boolean coerceToBoolean(Context cx, CallFrameV2 frame) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.mozilla.javascript.interpreterv2.operand;

import org.mozilla.javascript.CallFrameV2;
import org.mozilla.javascript.Context;

public final class OneOperand extends Operand {
public static final OneOperand instance = new OneOperand();

private OneOperand() {}

@Override
public Integer retrieve(Context cx, CallFrameV2 frame) {
return Integer.valueOf(1);
}

@Override
public double retrieveDouble(CallFrameV2 frame) {
throw new UnsupportedOperationException();
}

@Override
public boolean isDouble(CallFrameV2 frame) {
return false;
}

@Override
public void appendDebugString(StringBuilder sb) {
sb.append("1");
}

@Override
public boolean isValidJumpTableKey() {
return true;
}

@Override
public boolean coerceToBoolean(Context cx, CallFrameV2 frame) {
return true;
}
}
Loading
Loading