refactor: use classes in index.js
This commit is contained in:
parent
077e8afff7
commit
d7b50fa45b
@ -254,11 +254,6 @@ public class SimpleRepl {
|
|||||||
res.defineOwnMember(env, "parse", new NativeFunction(args -> {
|
res.defineOwnMember(env, "parse", new NativeFunction(args -> {
|
||||||
return JSONConverter.toJs(JSON.parse(null, args.get(0).toString(env)));
|
return JSONConverter.toJs(JSON.parse(null, args.get(0).toString(env)));
|
||||||
}));
|
}));
|
||||||
res.defineOwnMember(env, "setConstructable", new NativeFunction(args -> {
|
|
||||||
var func = (FunctionValue)args.get(0);
|
|
||||||
func.enableNew = args.get(1).toBoolean();
|
|
||||||
return Value.UNDEFINED;
|
|
||||||
}));
|
|
||||||
res.defineOwnMember(env, "invokeType", new NativeFunction(args -> {
|
res.defineOwnMember(env, "invokeType", new NativeFunction(args -> {
|
||||||
if (((ArgumentsValue)args.get(0)).frame.isNew) return StringValue.of("new");
|
if (((ArgumentsValue)args.get(0)).frame.isNew) return StringValue.of("new");
|
||||||
else return StringValue.of("call");
|
else return StringValue.of("call");
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
return;
|
// return;
|
||||||
|
|
||||||
const target = arguments[0];
|
const target = arguments[0];
|
||||||
const primordials = arguments[1];
|
const primordials = arguments[1];
|
||||||
@ -60,8 +60,7 @@ const undefined = ({}).definitelyDefined;
|
|||||||
|
|
||||||
target.undefined = undefined;
|
target.undefined = undefined;
|
||||||
|
|
||||||
const unwrapThis = (self, type, constr, name, arg, defaultVal) => {
|
const unwrapThis = (self, type, constr, name, arg = "this", defaultVal) => {
|
||||||
if (arg == null) arg = "this";
|
|
||||||
if (typeof self === type) return self;
|
if (typeof self === type) return self;
|
||||||
if (self instanceof constr && valueKey in self) self = self[valueKey];
|
if (self instanceof constr && valueKey in self) self = self[valueKey];
|
||||||
if (typeof self === type) return self;
|
if (typeof self === type) return self;
|
||||||
@ -72,14 +71,31 @@ const unwrapThis = (self, type, constr, name, arg, defaultVal) => {
|
|||||||
|
|
||||||
const wrapIndex = (i, len) => {};
|
const wrapIndex = (i, len) => {};
|
||||||
|
|
||||||
const Symbol = (name = "") => symbol.makeSymbol(name);
|
class Symbol {
|
||||||
|
get description() {
|
||||||
|
return symbol.getSymbolDescriptor(unwrapThis(this, "symbol", Symbol, "Symbol.prototype.description"));
|
||||||
|
}
|
||||||
|
toString() {
|
||||||
|
return "Symbol(" + unwrapThis(this, "symbol", Symbol, "Symbol.prototype.toString").description + ")";
|
||||||
|
}
|
||||||
|
valueOf() {
|
||||||
|
return unwrapThis(this, "symbol", Symbol, "Symbol.prototype.valueOf");
|
||||||
|
}
|
||||||
|
|
||||||
defineField(Symbol, "for", true, false, true, function(name) {
|
constructor(name = "") {
|
||||||
|
return symbol.makeSymbol(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
static for(name) {
|
||||||
return symbol.getSymbol(name + "");
|
return symbol.getSymbol(name + "");
|
||||||
});
|
}
|
||||||
defineField(Symbol, "keyFor", true, false, true, function(value) {
|
static keyFor(value) {
|
||||||
return symbol.getSymbolKey(unwrapThis(value, "symbol", Symbol, "Symbol.keyFor"));
|
return symbol.getSymbolKey(unwrapThis(value, "symbol", Symbol, "Symbol.keyFor"));
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setCallable(Symbol, true);
|
||||||
|
setConstructable(Symbol, false);
|
||||||
|
|
||||||
defineField(Symbol, "asyncIterator", false, false, false, Symbol("Symbol.asyncIterator"));
|
defineField(Symbol, "asyncIterator", false, false, false, Symbol("Symbol.asyncIterator"));
|
||||||
defineField(Symbol, "iterator", false, false, false, Symbol("Symbol.iterator"));
|
defineField(Symbol, "iterator", false, false, false, Symbol("Symbol.iterator"));
|
||||||
@ -89,61 +105,60 @@ defineField(Symbol, "replace", false, false, false, Symbol("Symbol.replace"));
|
|||||||
defineField(Symbol, "search", false, false, false, Symbol("Symbol.search"));
|
defineField(Symbol, "search", false, false, false, Symbol("Symbol.search"));
|
||||||
defineField(Symbol, "split", false, false, false, Symbol("Symbol.split"));
|
defineField(Symbol, "split", false, false, false, Symbol("Symbol.split"));
|
||||||
defineField(Symbol, "toStringTag", false, false, false, Symbol("Symbol.toStringTag"));
|
defineField(Symbol, "toStringTag", false, false, false, Symbol("Symbol.toStringTag"));
|
||||||
defineField(Symbol, "prototype", false, false, false, {});
|
|
||||||
|
|
||||||
defineProperty(Symbol.prototype, "description", false, true, function () {
|
|
||||||
return symbol.getSymbolDescription(unwrapThis(this, "symbol", Symbol, "Symbol.prototype.description"));
|
|
||||||
}, undefined);
|
|
||||||
defineField(Symbol.prototype, "toString", true, false, true, function() {
|
|
||||||
return "Symbol(" + unwrapThis(this, "symbol", Symbol, "Symbol.prototype.toString").description + ")";
|
|
||||||
});
|
|
||||||
defineField(Symbol.prototype, "valueOf", true, false, true, function() {
|
|
||||||
return unwrapThis(this, "symbol", Symbol, "Symbol.prototype.valueOf");
|
|
||||||
});
|
|
||||||
|
|
||||||
|
Symbol();
|
||||||
target.Symbol = Symbol;
|
target.Symbol = Symbol;
|
||||||
|
|
||||||
const Number = function(value) {
|
class Number {
|
||||||
|
toString() {
|
||||||
|
return "" + unwrapThis(this, "number", Number, "Number.prototype.toString");
|
||||||
|
}
|
||||||
|
valueOf() {
|
||||||
|
return unwrapThis(this, "number", Number, "Number.prototype.toString");
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(value) {
|
||||||
if (invokeType(arguments) === "call") {
|
if (invokeType(arguments) === "call") {
|
||||||
if (arguments.length === 0) return 0;
|
if (arguments.length === 0) return 0;
|
||||||
else return +value;
|
else return +value;
|
||||||
}
|
}
|
||||||
|
|
||||||
this[valueKey] = target.Number(value);
|
this[valueKey] = target.Number(value);
|
||||||
};
|
}
|
||||||
|
|
||||||
defineField(Number, "isFinite", true, false, true, function(value) {
|
static isFinite(value) {
|
||||||
value = unwrapThis(value, "number", Number, "Number.isFinite", "value", undefined);
|
value = unwrapThis(value, "number", Number, "Number.isFinite", "value", undefined);
|
||||||
|
|
||||||
if (value === undefined || value !== value) return false;
|
if (value === undefined || value !== value) return false;
|
||||||
if (value === Infinity || value === -Infinity) return false;
|
if (value === Infinity || value === -Infinity) return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
});
|
}
|
||||||
defineField(Number, "isInteger", true, false, true, function(value) {
|
static isInteger(value) {
|
||||||
value = unwrapThis(value, "number", Number, "Number.isInteger", "value", undefined);
|
value = unwrapThis(value, "number", Number, "Number.isInteger", "value", undefined);
|
||||||
if (value === undefined) return false;
|
if (value === undefined) return false;
|
||||||
return number.parseInt(value) === value;
|
return number.parseInt(value) === value;
|
||||||
});
|
}
|
||||||
defineField(Number, "isNaN", true, false, true, function(value) {
|
static isNaN(value) {
|
||||||
return number.isNaN(value);
|
return number.isNaN(value);
|
||||||
});
|
}
|
||||||
defineField(Number, "isSafeInteger", true, false, true, function(value) {
|
static isSafeInteger(value) {
|
||||||
value = unwrapThis(value, "number", Number, "Number.isSafeInteger", "value", undefined);
|
value = unwrapThis(value, "number", Number, "Number.isSafeInteger", "value", undefined);
|
||||||
if (value === undefined || number.parseInt(value) !== value) return false;
|
if (value === undefined || number.parseInt(value) !== value) return false;
|
||||||
return value >= -9007199254740991 && value <= 9007199254740991;
|
return value >= -9007199254740991 && value <= 9007199254740991;
|
||||||
});
|
}
|
||||||
defineField(Number, "parseFloat", true, false, true, function(value) {
|
static parseFloat(value) {
|
||||||
value = 0 + value;
|
value = 0 + value;
|
||||||
return number.parseFloat(value);
|
return number.parseFloat(value);
|
||||||
});
|
}
|
||||||
defineField(Number, "parseInt", true, false, true, function(value, radix) {
|
static parseInt(value, radix) {
|
||||||
value = 0 + value;
|
value = 0 + value;
|
||||||
radix = +radix;
|
radix = +radix;
|
||||||
if (number.isNaN(radix)) radix = 10;
|
if (number.isNaN(radix)) radix = 10;
|
||||||
|
|
||||||
return number.parseInt(value, radix);
|
return number.parseInt(value, radix);
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
|
||||||
defineField(Number, "EPSILON", false, false, false, 2.220446049250313e-16);
|
defineField(Number, "EPSILON", false, false, false, 2.220446049250313e-16);
|
||||||
defineField(Number, "MIN_SAFE_INTEGER", false, false, false, -9007199254740991);
|
defineField(Number, "MIN_SAFE_INTEGER", false, false, false, -9007199254740991);
|
||||||
@ -153,32 +168,36 @@ defineField(Number, "NEGATIVE_INFINITY", false, false, false, -number.Infinity);
|
|||||||
defineField(Number, "NaN", false, false, false, number.NaN);
|
defineField(Number, "NaN", false, false, false, number.NaN);
|
||||||
defineField(Number, "MAX_VALUE", false, false, false, 1.7976931348623157e+308);
|
defineField(Number, "MAX_VALUE", false, false, false, 1.7976931348623157e+308);
|
||||||
defineField(Number, "MIN_VALUE", false, false, false, 5e-324);
|
defineField(Number, "MIN_VALUE", false, false, false, 5e-324);
|
||||||
defineField(Number, "prototype", false, false, false, {});
|
|
||||||
|
|
||||||
defineField(Number.prototype, "toString", true, false, true);
|
|
||||||
defineField(Number.prototype, "toString", true, false, true, function() {
|
|
||||||
return "" + unwrapThis(this, "number", Number, "Number.prototype.toString");
|
|
||||||
});
|
|
||||||
defineField(Number.prototype, "valueOf", true, false, true, function() {
|
|
||||||
return unwrapThis(this, "number", Number, "Number.prototype.toString");
|
|
||||||
});
|
|
||||||
|
|
||||||
|
setCallable(Number, true);
|
||||||
target.Number = Number;
|
target.Number = Number;
|
||||||
target.parseInt = Number.parseInt;
|
target.parseInt = Number.parseInt;
|
||||||
target.parseFloat = Number.parseFloat;
|
target.parseFloat = Number.parseFloat;
|
||||||
target.NaN = Number.NaN;
|
target.NaN = Number.NaN;
|
||||||
target.Infinity = Number.POSITIVE_INFINITY;
|
target.Infinity = Number.POSITIVE_INFINITY;
|
||||||
|
|
||||||
const String = function(value) {
|
class String {
|
||||||
|
at(index) {
|
||||||
|
throw "Not implemented :/";
|
||||||
|
return unwrapThis(this, "string", String, "String.prototype.at")[index];
|
||||||
|
}
|
||||||
|
toString() {
|
||||||
|
return unwrapThis(this, "string", String, "String.prototype.toString");
|
||||||
|
}
|
||||||
|
valueOf() {
|
||||||
|
return unwrapThis(this, "string", String, "String.prototype.valueOf");
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(value) {
|
||||||
if (invokeType(arguments) === "call") {
|
if (invokeType(arguments) === "call") {
|
||||||
if (arguments.length === 0) return "";
|
if (arguments.length === 0) return "";
|
||||||
else return value + "";
|
else return value + "";
|
||||||
}
|
}
|
||||||
|
|
||||||
this[valueKey] = String(value);
|
this[valueKey] = String(value);
|
||||||
};
|
}
|
||||||
|
|
||||||
defineField(String, "fromCharCode", true, false, true, function() {
|
static fromCharCode() {
|
||||||
const res = [];
|
const res = [];
|
||||||
res[arguments.length] = 0;
|
res[arguments.length] = 0;
|
||||||
|
|
||||||
@ -187,8 +206,8 @@ defineField(String, "fromCharCode", true, false, true, function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return stringBuild(res);
|
return stringBuild(res);
|
||||||
});
|
}
|
||||||
defineField(String, "fromCodePoint", true, false, true, function() {
|
static fromCodePoint() {
|
||||||
const res = [];
|
const res = [];
|
||||||
res[arguments.length] = 0;
|
res[arguments.length] = 0;
|
||||||
|
|
||||||
@ -197,44 +216,50 @@ defineField(String, "fromCodePoint", true, false, true, function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return stringBuild(res);
|
return stringBuild(res);
|
||||||
});
|
}
|
||||||
|
}
|
||||||
defineField(String, "prototype", false, false, false, {});
|
|
||||||
|
|
||||||
defineField(String.prototype, "at", true, false, true, function(index) {
|
|
||||||
throw "Not implemented :/";
|
|
||||||
return unwrapThis(this, "string", String, "String.prototype.at")[index];
|
|
||||||
});
|
|
||||||
defineField(String.prototype, "toString", true, false, true, function() {
|
|
||||||
return unwrapThis(this, "string", String, "String.prototype.toString");
|
|
||||||
});
|
|
||||||
defineField(String.prototype, "valueOf", true, false, true, function() {
|
|
||||||
return unwrapThis(this, "string", String, "String.prototype.valueOf");
|
|
||||||
});
|
|
||||||
|
|
||||||
|
setCallable(String, true);
|
||||||
target.String = String;
|
target.String = String;
|
||||||
|
|
||||||
const Boolean = function(value) {
|
class Boolean {
|
||||||
|
toString() {
|
||||||
|
return "" + unwrapThis(this, "boolean", Boolean, "Boolean.prototype.toString");
|
||||||
|
}
|
||||||
|
valueOf() {
|
||||||
|
return unwrapThis(this, "boolean", Boolean, "Boolean.prototype.valueOf");
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(value) {
|
||||||
if (invokeType(arguments) === "call") {
|
if (invokeType(arguments) === "call") {
|
||||||
if (arguments.length === 0) return false;
|
if (arguments.length === 0) return false;
|
||||||
else return !!value;
|
else return !!value;
|
||||||
}
|
}
|
||||||
|
|
||||||
this[valueKey] = Boolean(value);
|
this[valueKey] = Boolean(value);
|
||||||
};
|
}
|
||||||
|
}
|
||||||
defineField(Boolean, "prototype", false, false, false, {});
|
|
||||||
|
|
||||||
defineField(Boolean.prototype, "toString", true, false, true, function() {
|
|
||||||
return "" + unwrapThis(this, "boolean", Boolean, "Boolean.prototype.toString");
|
|
||||||
});
|
|
||||||
defineField(Boolean.prototype, "valueOf", true, false, true, function() {
|
|
||||||
return unwrapThis(this, "boolean", Boolean, "Boolean.prototype.valueOf");
|
|
||||||
});
|
|
||||||
|
|
||||||
|
setCallable(Boolean, true);
|
||||||
target.Boolean = Boolean;
|
target.Boolean = Boolean;
|
||||||
|
|
||||||
const Object = function(value) {
|
class Object {
|
||||||
|
toString() {
|
||||||
|
print("2");
|
||||||
|
if (this !== null && this !== undefined && (Symbol.toStringTag in this)) return "[object " + this[Symbol.toStringTag] + "]";
|
||||||
|
else if (typeof this === "number" || this instanceof Number) return "[object Number]";
|
||||||
|
else if (typeof this === "symbol" || this instanceof Symbol) return "[object Symbol]";
|
||||||
|
else if (typeof this === "string" || this instanceof String) return "[object String]";
|
||||||
|
else if (typeof this === "boolean" || this instanceof Boolean) return "[object Boolean]";
|
||||||
|
else if (typeof this === "function") return "[object Function]";
|
||||||
|
else return "[object Object]";
|
||||||
|
}
|
||||||
|
valueOf() {
|
||||||
|
print("1");
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(value) {
|
||||||
if (typeof value === 'object' && value !== null) return value;
|
if (typeof value === 'object' && value !== null) return value;
|
||||||
|
|
||||||
if (typeof value === 'string') return new String(value);
|
if (typeof value === 'string') return new String(value);
|
||||||
@ -248,14 +273,13 @@ const Object = function(value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const target = this;
|
const target = this;
|
||||||
|
// TODO: use new.target.prototype as proto
|
||||||
if (target == null || typeof target !== 'object') target = {};
|
if (target == null || typeof target !== 'object') target = {};
|
||||||
|
|
||||||
this[valueKey] = Object(value);
|
this[valueKey] = Object(value);
|
||||||
};
|
}
|
||||||
|
|
||||||
defineField(Object, "prototype", false, false, false, setPrototype({}, null));
|
static defineProperty(obj, key, desc) {
|
||||||
|
|
||||||
defineField(Object, "defineProperty", true, false, true, (obj, key, desc) => {
|
|
||||||
if (typeof obj !== "object" || obj === null) {
|
if (typeof obj !== "object" || obj === null) {
|
||||||
print(obj);
|
print(obj);
|
||||||
print(typeof obj);
|
print(typeof obj);
|
||||||
@ -284,24 +308,20 @@ defineField(Object, "defineProperty", true, false, true, (obj, key, desc) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
});
|
}
|
||||||
|
}
|
||||||
defineField(Object.prototype, "toString", true, false, true, function() {
|
|
||||||
if (this !== null && this !== undefined && (Symbol.toStringTag in this)) return "[object " + this[Symbol.toStringTag] + "]";
|
|
||||||
else if (typeof this === "number" || this instanceof Number) return "[object Number]";
|
|
||||||
else if (typeof this === "symbol" || this instanceof Symbol) return "[object Symbol]";
|
|
||||||
else if (typeof this === "string" || this instanceof String) return "[object String]";
|
|
||||||
else if (typeof this === "boolean" || this instanceof Boolean) return "[object Boolean]";
|
|
||||||
else if (typeof this === "function") return "[object Function]";
|
|
||||||
else return "[object Object]";
|
|
||||||
});
|
|
||||||
defineField(Object.prototype, "valueOf", true, false, true, function() {
|
|
||||||
return this;
|
|
||||||
});
|
|
||||||
|
|
||||||
|
setCallable(Object, true);
|
||||||
|
setPrototype(Object.prototype, null);
|
||||||
target.Object = Object;
|
target.Object = Object;
|
||||||
|
|
||||||
const Function = function() {
|
class Function {
|
||||||
|
toString() {
|
||||||
|
if (this.name !== "") return "function " + this.name + "(...) { ... }";
|
||||||
|
else return "function (...) { ... }";
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor() {
|
||||||
const parts = ["return function annonymous("];
|
const parts = ["return function annonymous("];
|
||||||
|
|
||||||
for (let i = 0; i < arguments.length - 1; i++) {
|
for (let i = 0; i < arguments.length - 1; i++) {
|
||||||
@ -314,27 +334,24 @@ const Function = function() {
|
|||||||
|
|
||||||
const res = compile(stringBuild(parts))();
|
const res = compile(stringBuild(parts))();
|
||||||
return res;
|
return res;
|
||||||
};
|
}
|
||||||
|
|
||||||
defineField(Function, "compile", true, false, true, (src = "", options = {}) => {
|
|
||||||
if (options.globals == null) options.globals = [];
|
|
||||||
if (options.wrap == null) options.wrap = true;
|
|
||||||
|
|
||||||
|
static compile(src = "", { globals = [], wrap = false } = {}) {
|
||||||
const parts = [];
|
const parts = [];
|
||||||
|
|
||||||
if (options.wrap) parts[parts.length] = "return (function() {\n";
|
if (wrap) parts[parts.length] = "return (function() {\n";
|
||||||
if (options.globals.length > 0) {
|
if (globals.length > 0) {
|
||||||
parts[parts.length] = "var ";
|
parts[parts.length] = "var ";
|
||||||
|
|
||||||
for (let i = 0; i < options.globals.length; i++) {
|
for (let i = 0; i < globals.length; i++) {
|
||||||
if (i > 0) parts[parts.length] = ",";
|
if (i > 0) parts[parts.length] = ",";
|
||||||
parts[parts.length] = options.globals[i];
|
parts[parts.length] = globals[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
parts[parts.length] = ";((g=arguments[0])=>{";
|
parts[parts.length] = ";((g=arguments[0])=>{";
|
||||||
|
|
||||||
for (let i = 0; i < options.globals.length; i++) {
|
for (let i = 0; i < globals.length; i++) {
|
||||||
const name = options.globals[i];
|
const name = globals[i];
|
||||||
parts[parts.length] = name + "=g[" + json.stringify(name) + "];";
|
parts[parts.length] = name + "=g[" + json.stringify(name) + "];";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -342,78 +359,97 @@ defineField(Function, "compile", true, false, true, (src = "", options = {}) =>
|
|||||||
}
|
}
|
||||||
|
|
||||||
parts[parts.length] = src;
|
parts[parts.length] = src;
|
||||||
if (options.wrap) parts[parts.length] = "\n})(arguments[0])";
|
if (wrap) parts[parts.length] = "\n})(arguments[0])";
|
||||||
|
|
||||||
const res = compile(stringBuild(parts));
|
const res = compile(stringBuild(parts));
|
||||||
return res;
|
return res;
|
||||||
});
|
}
|
||||||
defineField(Function, "prototype", false, false, false, setPrototype({}, null));
|
}
|
||||||
|
|
||||||
defineField(Function.prototype, "toString", true, false, true, function() {
|
|
||||||
if (this.name !== "") return "function " + this.name + "(...) { ... }";
|
|
||||||
else return "function (...) { ... }";
|
|
||||||
});
|
|
||||||
defineField(Function.prototype, "valueOf", true, false, true, function() {
|
|
||||||
return this;
|
|
||||||
});
|
|
||||||
|
|
||||||
|
setCallable(Function, true);
|
||||||
target.Function = Function;
|
target.Function = Function;
|
||||||
|
|
||||||
// setIntrinsic("spread_obj", target.spread_obj = (target, obj) => {
|
class Array {
|
||||||
// if (obj === null || obj === undefined) return;
|
constructor(len) {
|
||||||
// const members = getOwnMembers(obj, true);
|
if (arguments.length === 1 && typeof len === "number") {
|
||||||
// const symbols = getOwnSymbolMembers(obj, true);
|
const res = [];
|
||||||
|
res.length = len;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
// TODO: Implement spreading
|
||||||
|
else throw new Error("Spreading not implemented");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// for (let i = 0; i < members.length; i++) {
|
setCallable(Array, true);
|
||||||
// const member = members[i];
|
target.Array = Array;
|
||||||
// target[member] = obj[member];
|
|
||||||
// }
|
|
||||||
|
|
||||||
// for (let i = 0; i < symbols.length; i++) {
|
class Error {
|
||||||
// const member = symbols[i];
|
toString() {
|
||||||
// target[member] = obj[member];
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// setIntrinsic("apply", target.spread_call = (func, self, args) => {
|
|
||||||
// return invoke(func, self, args);
|
|
||||||
// });
|
|
||||||
// setIntrinsic("apply", target.spread_new = (func, args) => {
|
|
||||||
// return invoke(func, null, args);
|
|
||||||
// });
|
|
||||||
|
|
||||||
const Error = function(msg = "") {
|
|
||||||
if (invokeType(arguments) === "call") return new Error(msg);
|
|
||||||
this.message = msg + "";
|
|
||||||
};
|
|
||||||
defineField(Error.prototype, "name", true, false, true, "Error");
|
|
||||||
defineField(Error.prototype, "message", true, false, true, "");
|
|
||||||
defineField(Error.prototype, "toString", true, false, true, function toString() {
|
|
||||||
let res = this.name || "Error";
|
let res = this.name || "Error";
|
||||||
|
|
||||||
const msg = this.message;
|
const msg = this.message;
|
||||||
if (msg) res += ": " + msg;
|
if (msg) res += ": " + msg;
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
});
|
}
|
||||||
|
|
||||||
|
constructor (msg = "") {
|
||||||
|
if (invokeType(arguments) === "call") return new Error(msg);
|
||||||
|
this.message = msg + "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
defineField(Error.prototype, "name", true, false, true, "Error");
|
||||||
|
defineField(Error.prototype, "message", true, false, true, "");
|
||||||
|
setCallable(Error, true);
|
||||||
target.Error = Error;
|
target.Error = Error;
|
||||||
|
|
||||||
const SyntaxError = function(msg = "") {
|
class SyntaxError {
|
||||||
|
constructor (msg = "") {
|
||||||
if (invokeType(arguments) === "call") return new SyntaxError(msg);
|
if (invokeType(arguments) === "call") return new SyntaxError(msg);
|
||||||
this.message = msg + "";
|
this.message = msg + "";
|
||||||
};
|
}
|
||||||
defineField(SyntaxError.prototype, "name", true, false, true, "SyntaxError");
|
}
|
||||||
|
|
||||||
|
defineField(SyntaxError.prototype, "name", true, false, true, "SyntaxError");
|
||||||
setPrototype(SyntaxError, Error);
|
setPrototype(SyntaxError, Error);
|
||||||
setPrototype(SyntaxError.prototype, Error.prototype);
|
setPrototype(SyntaxError.prototype, Error.prototype);
|
||||||
|
setCallable(SyntaxError, true);
|
||||||
target.SyntaxError = SyntaxError;
|
target.SyntaxError = SyntaxError;
|
||||||
|
|
||||||
|
class TypeError {
|
||||||
|
constructor (msg = "") {
|
||||||
|
if (invokeType(arguments) === "call") return new TypeError(msg);
|
||||||
|
this.message = msg + "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
defineField(TypeError.prototype, "name", true, false, true, "TypeError");
|
||||||
|
setPrototype(TypeError, Error);
|
||||||
|
setPrototype(TypeError.prototype, Error.prototype);
|
||||||
|
setCallable(TypeError, true);
|
||||||
|
target.TypeError = TypeError;
|
||||||
|
|
||||||
|
class RangeError {
|
||||||
|
constructor (msg = "") {
|
||||||
|
if (invokeType(arguments) === "call") return new RangeError(msg);
|
||||||
|
this.message = msg + "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
defineField(RangeError.prototype, "name", true, false, true, "RangeError");
|
||||||
|
setPrototype(RangeError, Error);
|
||||||
|
setPrototype(RangeError.prototype, Error.prototype);
|
||||||
|
setCallable(RangeError, true);
|
||||||
|
target.RangeError = RangeError;
|
||||||
|
|
||||||
setGlobalPrototype("string", String.prototype);
|
setGlobalPrototype("string", String.prototype);
|
||||||
setGlobalPrototype("number", Number.prototype);
|
setGlobalPrototype("number", Number.prototype);
|
||||||
setGlobalPrototype("boolean", Boolean.prototype);
|
setGlobalPrototype("boolean", Boolean.prototype);
|
||||||
setGlobalPrototype("symbol", Symbol.prototype);
|
setGlobalPrototype("symbol", Symbol.prototype);
|
||||||
setGlobalPrototype("object", Object.prototype);
|
setGlobalPrototype("object", Object.prototype);
|
||||||
|
setGlobalPrototype("array", Array.prototype);
|
||||||
setGlobalPrototype("function", Function.prototype);
|
setGlobalPrototype("function", Function.prototype);
|
||||||
setGlobalPrototype("error", Error.prototype);
|
setGlobalPrototype("error", Error.prototype);
|
||||||
setGlobalPrototype("syntax", SyntaxError.prototype);
|
setGlobalPrototype("syntax", SyntaxError.prototype);
|
||||||
|
Loading…
Reference in New Issue
Block a user