Make narcissus use ES5 defineProperty (bug 546532, r=dherman)

This commit is contained in:
Andreas Gal 2010-07-14 16:23:21 -07:00
Родитель 506520abe3
Коммит e8c107bcad
4 изменённых файлов: 77 добавлений и 79 удалений

Просмотреть файл

@ -1,4 +1,7 @@
/* -*- Mode: JS; tab-width: 4; indent-tabs-mode: nil; -*-
* vim: set sw=4 ts=8 et tw=78:
/* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
@ -46,9 +49,6 @@
* catch guards
* const declarations
* get and set functions in object initialisers
* Object.prototype.__defineGetter__
* Object.prototype.__defineSetter__
* Object.prototype.__defineProperty__
* Object.prototype.__proto__
* filename and line number arguments to *Error constructors
* callable regular expression objects
@ -59,16 +59,8 @@
* get and set functions in object initialisers
*/
/*
* Loads a file relative to the calling script's (our) source directory, and not
* the directory that the executing shell is being run out of.
*/
function my_load(filename) {
evaluate(snarf(filename), filename, 1);
}
my_load('jsdefs.js');
my_load('jslex.js');
my_load('jsparse.js');
my_load('jsexec.js');
load('jsdefs.js');
load('jslex.js');
load('jsparse.js');
load('jsexec.js');

Просмотреть файл

@ -168,3 +168,11 @@ for (i = 0, j = assignOps.length; i < j; i++) {
t = assignOps[i];
assignOps[t] = tokens[t];
}
function defineGetter(obj, prop, fn, dontDelete, dontEnum) {
Object.defineProperty(obj, prop, { get: fn, configurable: !dontDelete, enumerable: !dontEnum });
}
function defineProperty(obj, prop, val, dontDelete, readOnly, dontEnum) {
Object.defineProperty(obj, prop, { value: val, writable: !readOnly, configurable: !dontDelete, enumerable: !dontEnum });
}

Просмотреть файл

@ -1,5 +1,6 @@
/* -*- Mode: JS; tab-width: 4; indent-tabs-mode: nil; -*-
* vim: set sw=4 ts=8 et tw=78:
/* ***** BEGIN LICENSE BLOCK *****
* vim: set ts=4 sw=4 et tw=80:
*
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
@ -153,8 +154,8 @@ function hasDirectProperty(o, p) {
// Reflect a host class into the target global environment by delegation.
function reflectClass(name, proto) {
var gctor = global[name];
gctor.__defineProperty__('prototype', proto, true, true, true);
proto.__defineProperty__('constructor', gctor, false, false, true);
defineProperty(gctor, "prototype", proto, true, true, true);
defineProperty(proto, "constructor", gctor, false, false, true);
return proto;
}
@ -246,13 +247,13 @@ function execute(n, x) {
if (!n.name || n.functionForm == STATEMENT_FORM) {
v = new FunctionObject(n, x.scope);
if (n.functionForm == STATEMENT_FORM)
x.scope.object.__defineProperty__(n.name, v, true);
defineProperty(x.scope.object, n.name, v, true);
} else {
t = new Object;
x.scope = {object: t, parent: x.scope};
try {
v = new FunctionObject(n, x.scope);
t.__defineProperty__(n.name, v, true, true);
defineProperty(t, n.name, v, true, true);
} finally {
x.scope = x.scope.parent;
}
@ -266,7 +267,7 @@ function execute(n, x) {
for (i = 0, j = a.length; i < j; i++) {
s = a[i].name;
f = new FunctionObject(a[i], x.scope);
t.__defineProperty__(s, f, x.type != EVAL_CODE);
defineProperty(t, s, f, x.type != EVAL_CODE);
}
a = n.varDecls;
for (i = 0, j = a.length; i < j; i++) {
@ -277,8 +278,7 @@ function execute(n, x) {
u.filename, u.lineno);
}
if (u.readOnly || !hasDirectProperty(t, s)) {
t.__defineProperty__(s, undefined, x.type != EVAL_CODE,
u.readOnly);
defineProperty(t, s, undefined, x.type != EVAL_CODE, u.readOnly);
}
}
// FALL THROUGH
@ -406,7 +406,7 @@ function execute(n, x) {
}
t = n.catchClauses[i];
x.scope = {object: {}, parent: x.scope};
x.scope.object.__defineProperty__(t.varName, e, true);
defineProperty(x.scope.object, t.varName, e, true);
try {
if (t.guard && !getValue(execute(t.guard, x)))
continue;
@ -454,7 +454,7 @@ function execute(n, x) {
}
u = getValue(execute(u, x));
if (n.type == CONST)
s.object.__defineProperty__(t, u, x.type != EVAL_CODE, true);
defineProperty(s.object, t, u, x.type != EVAL_CODE, true);
else
s.object[t] = u;
}
@ -668,9 +668,9 @@ function execute(n, x) {
v = {};
for (i = 0, j = n.length; i < j; i++) {
u = getValue(execute(n[i], x));
v.__defineProperty__(i, u, false, false, true);
defineProperty(v, i, u, false, false, true);
}
v.__defineProperty__('length', i, false, false, true);
defineProperty(v, "length", i, false, false, true);
break;
case CALL:
@ -693,7 +693,7 @@ function execute(n, x) {
f = getValue(r);
if (n.type == NEW) {
a = {};
a.__defineProperty__('length', 0, false, false, true);
defineProperty(a, "length", 0, false, false, true);
} else {
a = execute(n[1], x);
}
@ -771,26 +771,24 @@ function execute(n, x) {
function Activation(f, a) {
for (var i = 0, j = f.params.length; i < j; i++)
this.__defineProperty__(f.params[i], a[i], true);
this.__defineProperty__('arguments', a, true);
defineProperty(this, f.params[i], a[i], true);
defineProperty(this, "arguments", a, true);
}
// Null Activation.prototype's proto slot so that Object.prototype.* does not
// pollute the scope of heavyweight functions. Also delete its 'constructor'
// property so that it doesn't pollute function scopes. But first, we must
// copy __defineProperty__ down from Object.prototype.
// property so that it doesn't pollute function scopes.
Activation.prototype.__defineProperty__ = Object.prototype.__defineProperty__;
Activation.prototype.__proto__ = null;
delete Activation.prototype.constructor;
function FunctionObject(node, scope) {
this.node = node;
this.scope = scope;
this.__defineProperty__('length', node.params.length, true, true, true);
defineProperty(this, "length", node.params.length, true, true, true);
var proto = {};
this.__defineProperty__('prototype', proto, true);
proto.__defineProperty__('constructor', this, false, false, true);
defineProperty(this, "prototype", proto, true);
defineProperty(proto, "constructor", this, false, false, true);
}
var FOp = FunctionObject.prototype = {
@ -800,7 +798,7 @@ var FOp = FunctionObject.prototype = {
x2.thisObject = t || global;
x2.caller = x;
x2.callee = this;
a.__defineProperty__('callee', this, false, false, true);
defineProperty(a, "callee", this, false, false, true);
var f = this.node;
x2.scope = {object: new Activation(f, a), parent: this.scope};
@ -867,12 +865,12 @@ var FOp = FunctionObject.prototype = {
if (a === undefined || a === null) {
a = {};
a.__defineProperty__('length', 0, false, false, true);
defineProperty(a, "length", 0, false, false, true);
} else if (a instanceof Array) {
var v = {};
for (var i = 0, j = a.length; i < j; i++)
v.__defineProperty__(i, a[i], false, false, true);
v.__defineProperty__('length', i, false, false, true);
defineProperty(v, i, a[i], false, false, true);
defineProperty(v, "length", i, false, false, true);
a = v;
} else if (!(a instanceof Object)) {
// XXX check for a non-arguments object
@ -899,28 +897,30 @@ var Fp = Function.prototype;
var REp = RegExp.prototype;
if (!('__call__' in Fp)) {
Fp.__defineProperty__('__call__', function (t, a, x) {
// Curse ECMA yet again!
a = Array.prototype.splice.call(a, 0, a.length);
return this.apply(t, a);
}, true, true, true);
REp.__defineProperty__('__call__', function (t, a, x) {
a = Array.prototype.splice.call(a, 0, a.length);
return this.exec.apply(this, a);
}, true, true, true);
Fp.__defineProperty__('__construct__', function (a, x) {
a = Array.prototype.splice.call(a, 0, a.length);
return this.__applyConstructor__(a);
}, true, true, true);
defineProperty(Fp, "__call__",
function (t, a, x) {
// Curse ECMA yet again!
a = Array.prototype.splice.call(a, 0, a.length);
return this.apply(t, a);
}, true, true, true);
defineProperty(REp, "__call__",
function (t, a, x) {
a = Array.prototype.splice.call(a, 0, a.length);
return this.exec.apply(this, a);
}, true, true, true);
defineProperty(Fp, "__construct__",
function (a, x) {
a = Array.prototype.splice.call(a, 0, a.length);
return this.__applyConstructor__(a);
}, true, true, true);
// Since we use native functions such as Date along with host ones such
// as global.eval, we want both to be considered instances of the native
// Function constructor.
Fp.__defineProperty__('__hasInstance__', function (v) {
return v instanceof Function || v instanceof global.Function;
}, true, true, true);
defineProperty(Fp, "__hasInstance__",
function (v) {
return v instanceof Function || v instanceof global.Function;
}, true, true, true);
}
function thunk(f, x) {

Просмотреть файл

@ -1,5 +1,7 @@
/* vim: set sw=4 ts=8 et tw=78: */
/* ***** BEGIN LICENSE BLOCK *****
/* -*- Mode: JS; tab-width: 4; indent-tabs-mode: nil; -*-
* vim: set sw=4 ts=8 et tw=78:
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
@ -66,13 +68,10 @@ function Script(t, x) {
}
// Node extends Array, which we extend slightly with a top-of-stack method.
Array.prototype.__defineProperty__(
'top',
function () {
return this.length && this[this.length-1];
},
false, false, true
);
defineProperty(Array.prototype, "top",
function() {
return this.length && this[this.length-1];
}, false, false, true);
function Node(t, type) {
var token = t.token;
@ -133,19 +132,18 @@ Np.getSource = function () {
return this.tokenizer.source.slice(this.start, this.end);
};
Np.__defineGetter__('filename',
function () { return this.tokenizer.filename; });
defineGetter(Np, "filename",
function() {
return this.tokenizer.filename;
});
String.prototype.__defineProperty__(
'repeat',
function (n) {
var s = "", t = this + s;
while (--n >= 0)
s += t;
return s;
},
false, false, true
);
defineProperty(String.prototype, "repeat",
function(n) {
var s = "", t = this + s;
while (--n >= 0)
s += t;
return s;
}, false, false, true);
// Statement stack and nested statement handler.
function nest(t, x, node, func, end) {