зеркало из https://github.com/mozilla/pjs.git
Changes to get traditional functions with args. working
This commit is contained in:
Родитель
c53bc15ee2
Коммит
05c29118df
|
@ -1,22 +0,0 @@
|
|||
|
||||
import java.util.Hashtable;
|
||||
|
||||
class Environment {
|
||||
|
||||
JSObject scope = new JSObject("globals", null);
|
||||
|
||||
void enterNewScope(JSObject newScope)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
String print()
|
||||
{
|
||||
StringBuffer result = new StringBuffer("Globals contents :\n");
|
||||
result.append(scope.toString());
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
JSValue resultValue;
|
||||
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
class FunctionNode extends ExpressionNode {
|
||||
|
||||
FunctionNode(JSIdentifier aName, ControlNodeGroup aBody)
|
||||
{
|
||||
fn = new NativeFunction(aBody.getHead());
|
||||
name = aName;
|
||||
}
|
||||
|
||||
JSValue eval(Environment theEnv)
|
||||
{
|
||||
theEnv.scope.putProp(theEnv, name, fn);
|
||||
return fn;
|
||||
}
|
||||
|
||||
JSString name;
|
||||
NativeFunction fn;
|
||||
|
||||
}
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -1,27 +0,0 @@
|
|||
class JSName extends ExpressionNode {
|
||||
|
||||
JSName(JSIdentifier anID, int aScope)
|
||||
{
|
||||
id = anID;
|
||||
scope = aScope; // this is the scope that the name was used in
|
||||
}
|
||||
|
||||
String print(String indent)
|
||||
{
|
||||
return indent + "JSName : " + id.s + ", scope : " + scope + "\n";
|
||||
}
|
||||
|
||||
JSReference evalLHS(Environment theEnv)
|
||||
{
|
||||
return new JSReference(theEnv.scope, id);
|
||||
}
|
||||
|
||||
JSValue eval(Environment theEnv)
|
||||
{
|
||||
return theEnv.scope.getProp(theEnv, id);
|
||||
}
|
||||
|
||||
JSIdentifier id;
|
||||
int scope;
|
||||
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
|
||||
import java.util.Hashtable;
|
||||
|
||||
class JSObject extends JSValue {
|
||||
|
||||
static JSObject JSUndefined = new JSObject("undefined", null);
|
||||
|
||||
JSObject(String aValue, JSObject aPrototype)
|
||||
{
|
||||
value = aValue;
|
||||
prototype = aPrototype;
|
||||
}
|
||||
|
||||
String print(String indent)
|
||||
{
|
||||
return indent + "JSObject : " + value + "\n";
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return value + contents.toString();
|
||||
}
|
||||
|
||||
JSValue eval(Environment theEnv)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
JSValue typeof(Environment theEnv) {
|
||||
if (this == JSUndefined)
|
||||
return new JSString("undefined");
|
||||
else
|
||||
return new JSString("object");
|
||||
}
|
||||
|
||||
JSBoolean toJSBoolean(Environment theEnv) {
|
||||
return JSBoolean.JSTrue;
|
||||
}
|
||||
|
||||
JSDouble toJSDouble(Environment theEnv) {
|
||||
return toPrimitive(theEnv, "Number").toJSDouble(theEnv);
|
||||
}
|
||||
|
||||
JSValue getProp(Environment theEnv, JSString id)
|
||||
{
|
||||
Object v = contents.get(id.s);
|
||||
if (v == null)
|
||||
if (prototype == null)
|
||||
return JSUndefined;
|
||||
else
|
||||
return prototype.getProp(theEnv, id);
|
||||
else
|
||||
return (JSValue)v;
|
||||
}
|
||||
|
||||
JSValue putProp(Environment theEnv, JSString id, JSValue rV) {
|
||||
contents.put(id.s, rV);
|
||||
return rV;
|
||||
}
|
||||
|
||||
|
||||
Hashtable contents = new Hashtable();
|
||||
|
||||
String value;
|
||||
|
||||
JSObject prototype;
|
||||
}
|
|
@ -1,218 +0,0 @@
|
|||
class JSValue extends ExpressionNode {
|
||||
|
||||
String print(String indent)
|
||||
{
|
||||
return indent + "JSValue\n";
|
||||
}
|
||||
|
||||
JSReference evalLHS(Environment theEnv)
|
||||
{
|
||||
throw new RuntimeException("EvalLHS on non-lvalue");
|
||||
}
|
||||
|
||||
JSValue eval(Environment theEnv)
|
||||
{
|
||||
throw new RuntimeException("Eval on JSValue");
|
||||
}
|
||||
|
||||
JSValue unimplemented(String op)
|
||||
{
|
||||
throw new RuntimeException("unimplemented " + op + " called");
|
||||
}
|
||||
|
||||
JSValue gt(Environment theEnv, JSValue rV) {
|
||||
JSValue lV = toPrimitive(theEnv, "Number");
|
||||
rV = rV.toPrimitive(theEnv, "Number");
|
||||
if ((lV instanceof JSString) && (rV instanceof JSString))
|
||||
return lV.gt(theEnv, rV);
|
||||
else
|
||||
return lV.toJSDouble(theEnv).gt(theEnv, rV.toJSDouble(theEnv));
|
||||
}
|
||||
|
||||
JSValue ge(Environment theEnv, JSValue rV) {
|
||||
JSValue lV = toPrimitive(theEnv, "Number");
|
||||
rV = rV.toPrimitive(theEnv, "Number");
|
||||
if ((lV instanceof JSString) && (rV instanceof JSString))
|
||||
return lV.ge(theEnv, rV);
|
||||
else
|
||||
return lV.toJSDouble(theEnv).ge(theEnv, rV.toJSDouble(theEnv));
|
||||
}
|
||||
|
||||
JSValue lt(Environment theEnv, JSValue rV) {
|
||||
JSValue lV = toPrimitive(theEnv, "Number");
|
||||
rV = rV.toPrimitive(theEnv, "Number");
|
||||
if ((lV instanceof JSString) && (rV instanceof JSString))
|
||||
return lV.lt(theEnv, rV);
|
||||
else
|
||||
return lV.toJSDouble(theEnv).lt(theEnv, rV.toJSDouble(theEnv));
|
||||
}
|
||||
|
||||
JSValue le(Environment theEnv, JSValue rV) {
|
||||
JSValue lV = toPrimitive(theEnv, "Number");
|
||||
rV = rV.toPrimitive(theEnv, "Number");
|
||||
if ((lV instanceof JSString) && (rV instanceof JSString))
|
||||
return lV.le(theEnv, rV);
|
||||
else
|
||||
return lV.toJSDouble(theEnv).le(theEnv, rV.toJSDouble(theEnv));
|
||||
}
|
||||
|
||||
JSValue eq(Environment theEnv, JSValue rV) {
|
||||
JSValue lV = toPrimitive(theEnv, "Number");
|
||||
rV = rV.toPrimitive(theEnv, "Number");
|
||||
if ((lV instanceof JSString) && (rV instanceof JSString))
|
||||
return lV.eq(theEnv, rV);
|
||||
else
|
||||
return lV.toJSDouble(theEnv).eq(theEnv, rV.toJSDouble(theEnv));
|
||||
}
|
||||
|
||||
JSValue ne(Environment theEnv, JSValue rV) {
|
||||
JSValue lV = toPrimitive(theEnv, "Number");
|
||||
rV = rV.toPrimitive(theEnv, "Number");
|
||||
if ((lV instanceof JSString) && (rV instanceof JSString))
|
||||
return lV.ne(theEnv, rV);
|
||||
else
|
||||
return lV.toJSDouble(theEnv).ne(theEnv, rV.toJSDouble(theEnv));
|
||||
}
|
||||
|
||||
JSValue plus(Environment theEnv) {
|
||||
return toJSDouble(theEnv).plus(theEnv);
|
||||
}
|
||||
|
||||
JSValue minus(Environment theEnv) {
|
||||
return toJSDouble(theEnv).minus(theEnv);
|
||||
}
|
||||
|
||||
JSValue twiddle(Environment theEnv) {
|
||||
return toJSInteger(theEnv).twiddle(theEnv);
|
||||
}
|
||||
|
||||
JSValue bang(Environment theEnv) {
|
||||
return toJSBoolean(theEnv).bang(theEnv);
|
||||
}
|
||||
|
||||
JSValue typeof(Environment theEnv) {
|
||||
return unimplemented("typeof");
|
||||
}
|
||||
|
||||
JSValue add(Environment theEnv, JSValue rV) {
|
||||
JSValue lV = toPrimitive(theEnv, "");
|
||||
rV = rV.toPrimitive(theEnv, "");
|
||||
if ((lV instanceof JSString) || (rV instanceof JSString))
|
||||
return lV.add(theEnv, rV);
|
||||
else
|
||||
return lV.toJSDouble(theEnv).add(theEnv, rV);
|
||||
}
|
||||
|
||||
JSValue subtract(Environment theEnv, JSValue rV) {
|
||||
return toJSDouble(theEnv).subtract(theEnv, rV.toJSDouble(theEnv));
|
||||
}
|
||||
|
||||
JSValue multiply(Environment theEnv, JSValue rV) {
|
||||
return toJSDouble(theEnv).multiply(theEnv, rV.toJSDouble(theEnv));
|
||||
}
|
||||
|
||||
JSValue divide(Environment theEnv, JSValue rV) {
|
||||
return toJSDouble(theEnv).divide(theEnv, rV.toJSDouble(theEnv));
|
||||
}
|
||||
|
||||
JSValue remainder(Environment theEnv, JSValue rV) {
|
||||
return toJSDouble(theEnv).remainder(theEnv, rV.toJSDouble(theEnv));
|
||||
}
|
||||
|
||||
JSValue and(Environment theEnv, JSValue rV) {
|
||||
return toJSInteger(theEnv).and(theEnv, rV.toJSInteger(theEnv));
|
||||
}
|
||||
|
||||
JSValue or(Environment theEnv, JSValue rV) {
|
||||
return toJSInteger(theEnv).or(theEnv, rV.toJSInteger(theEnv));
|
||||
}
|
||||
|
||||
JSValue xor(Environment theEnv, JSValue rV) {
|
||||
return toJSInteger(theEnv).xor(theEnv, rV.toJSInteger(theEnv));
|
||||
}
|
||||
|
||||
JSValue shl(Environment theEnv, JSValue rV) {
|
||||
return toJSInteger(theEnv).shl(theEnv, rV.toJSInteger(theEnv));
|
||||
}
|
||||
|
||||
JSValue shr(Environment theEnv, JSValue rV) {
|
||||
return toJSInteger(theEnv).shr(theEnv, rV.toJSInteger(theEnv));
|
||||
}
|
||||
|
||||
JSValue ushl(Environment theEnv, JSValue rV) {
|
||||
return toJSInteger(theEnv).ushl(theEnv, rV.toJSInteger(theEnv));
|
||||
}
|
||||
|
||||
JSValue getProp(Environment theEnv, JSString id) {
|
||||
return toJSObject(theEnv).getProp(theEnv, id);
|
||||
}
|
||||
|
||||
JSValue putProp(Environment theEnv, JSString id, JSValue rV) {
|
||||
return toJSObject(theEnv).putProp(theEnv, id, rV);
|
||||
}
|
||||
|
||||
JSValue call(Environment theEnv, JSValue rV) {
|
||||
throw new JSException(new JSString("[[call]] not implemented"));
|
||||
}
|
||||
|
||||
JSValue defaultValue(Environment theEnv, String hint) {
|
||||
/*
|
||||
When the [[DefaultValue]] method of O is called with hint String, the following steps are taken:
|
||||
1. Call the [[Get]] method of object O with argument "toString".
|
||||
2. If Result(1) is not an object, go to step 5.
|
||||
3. Call the [[Call]] method of Result(1), with O as the this value and an empty argument list.
|
||||
4. If Result(3) is a primitive value, return Result(3).
|
||||
5. Call the [[Get]] method of object O with argument "valueOf".
|
||||
6. If Result(5) is not an object, go to step 9.
|
||||
7. Call the [[Call]] method of Result(5), with O as the this value and an empty argument list.
|
||||
8. If Result(7) is a primitive value, return Result(7).
|
||||
9. Generate a runtime error.
|
||||
*/
|
||||
JSValue v = null;
|
||||
if (hint.equals("String")) {
|
||||
v = getProp(theEnv, new JSString("toString"));
|
||||
if (v instanceof JSObject) {
|
||||
// invoke 'v.Call' with 'this' as the JS this
|
||||
}
|
||||
else {
|
||||
v = getProp(theEnv, new JSString("valueOf"));
|
||||
if (v instanceof JSObject) {
|
||||
}
|
||||
else
|
||||
throw new JSException(new JSString("No default value"));
|
||||
}
|
||||
}
|
||||
else { // hint.equals("Number")
|
||||
/*
|
||||
When the [[DefaultValue]] method of O is called with hint Number, the following steps are taken:
|
||||
1. Call the [[Get]] method of object O with argument "valueOf".
|
||||
2. If Result(1) is not an object, go to step 5.
|
||||
3. Call the [[Call]] method of Result(1), with O as the this value and an empty argument list.
|
||||
4. If Result(3) is a primitive value, return Result(3).
|
||||
5. Call the [[Get]] method of object O with argument "toString".
|
||||
6. If Result(5) is not an object, go to step 9.
|
||||
7. Call the [[Call]] method of Result(5), with O as the this value and an empty argument list.
|
||||
8. If Result(7) is a primitive value, return Result(7).
|
||||
9. Generate a runtime error.
|
||||
*/
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
JSValue toPrimitive(Environment theEnv, String hint) {
|
||||
JSValue result = defaultValue(theEnv, hint);
|
||||
if (result instanceof JSObject)
|
||||
throw new JSException(new JSString("default value returned object"));
|
||||
else
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
JSObject toJSObject(Environment theEnv) { unimplemented("toJSObjet"); return null; }
|
||||
JSDouble toJSDouble(Environment theEnv) { unimplemented("toJSDouble"); return null; }
|
||||
JSInteger toJSInteger(Environment theEnv) { unimplemented("toJSInteger"); return null; }
|
||||
JSString toJSString(Environment theEnv) { unimplemented("toJSString"); return null; }
|
||||
JSBoolean toJSBoolean(Environment theEnv) { unimplemented("toJSBoolean"); return null; }
|
||||
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
class NativeFunction extends JSObject {
|
||||
|
||||
NativeFunction(ControlNode aBody)
|
||||
{
|
||||
super("Function", null);
|
||||
body = aBody;
|
||||
}
|
||||
|
||||
JSValue call(Environment theEnv, JSValue rV)
|
||||
{
|
||||
ControlNode c = body;
|
||||
while (c != null) c = c.eval(theEnv);
|
||||
|
||||
return theEnv.resultValue;
|
||||
}
|
||||
|
||||
ControlNode body;
|
||||
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
class NativeNumber extends JSObject {
|
||||
|
||||
NativeNumber(double p) {
|
||||
super("Number", null);
|
||||
d = p;
|
||||
}
|
||||
|
||||
JSValue defaultValue(Environment theEnv, String hint) {
|
||||
if (hint.equals("String"))
|
||||
return new JSString(Double.toString(d));
|
||||
else
|
||||
return new JSDouble(d);
|
||||
}
|
||||
|
||||
double d;
|
||||
|
||||
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
|
||||
import java.util.Vector;
|
||||
|
||||
class TryNode extends ControlNode {
|
||||
|
||||
TryNode(ControlNode tryCode)
|
||||
{
|
||||
super(null);
|
||||
tryBody = tryCode;
|
||||
}
|
||||
|
||||
void addFinally(ControlNode finallyCode)
|
||||
{
|
||||
finallyBody = finallyCode;
|
||||
}
|
||||
|
||||
void addCatchClause(ExpressionNode e, ControlNode c)
|
||||
{
|
||||
catchExpr.addElement(e);
|
||||
catchCode.addElement(c);
|
||||
}
|
||||
|
||||
ControlNode eval(Environment theEnv)
|
||||
{
|
||||
try {
|
||||
ControlNode c = tryBody;
|
||||
while (c != null) c = c.eval(theEnv);
|
||||
}
|
||||
catch (JSException x) {
|
||||
int count = catchExpr.size();
|
||||
for (int i = 0; i < count; i++) {
|
||||
ExpressionNode e = (ExpressionNode)(catchExpr.elementAt(i));
|
||||
String id = ((JSObject)e).value;
|
||||
theEnv.scope.contents.put(id, x.getValue()); // XXX YAARGH !!!
|
||||
return (ControlNode)(catchCode.elementAt(i));
|
||||
}
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
Vector catchExpr = new Vector();
|
||||
Vector catchCode = new Vector();
|
||||
|
||||
ControlNode tryBody;
|
||||
ControlNode finallyBody;
|
||||
|
||||
}
|
Загрузка…
Ссылка в новой задаче