major changes in preparations for environments

This commit is contained in:
2023-09-04 14:30:57 +03:00
parent d1b37074a6
commit 29d3f378a5
54 changed files with 2512 additions and 2161 deletions

View File

@@ -11,20 +11,20 @@ define("values/array", () => {
res[i] = arguments[i];
}
}
return res;
} as ArrayConstructor;
Array.prototype = ([] as any).__proto__ as Array<any>;
setConstr(Array.prototype, Array, env);
env.setProto('array', Array.prototype);
(Array.prototype as any)[Symbol.typeName] = "Array";
setProps(Array.prototype, env, {
setConstr(Array.prototype, Array);
setProps(Array.prototype, {
[Symbol.iterator]: function() {
return this.values();
},
[Symbol.typeName]: "Array",
values() {
var i = 0;
@@ -67,7 +67,7 @@ define("values/array", () => {
concat() {
var res = [] as any[];
res.push.apply(res, this);
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i];
if (arg instanceof Array) {
@@ -296,7 +296,7 @@ define("values/array", () => {
if (typeof func !== 'function') throw new TypeError('Expected func to be undefined or a function.');
env.internals.sort(this, func);
internals.sort(this, func);
return this;
},
splice(start, deleteCount, ...items) {
@@ -328,8 +328,9 @@ define("values/array", () => {
return res;
}
});
setProps(Array, env, {
isArray(val: any) { return env.internals.isArr(val); }
setProps(Array, {
isArray(val: any) { return internals.isArray(val); }
});
internals.markSpecial(Array);
});

View File

@@ -7,6 +7,6 @@ define("values/boolean", () => {
else (this as any).value = val;
} as BooleanConstructor;
Boolean.prototype = (false as any).__proto__ as Boolean;
setConstr(Boolean.prototype, Boolean, env);
env.setProto('bool', Boolean.prototype);
setConstr(Boolean.prototype, Boolean);
});

View File

@@ -8,36 +8,39 @@ define("values/errors", () => {
stack: [] as string[],
}, Error.prototype);
} as ErrorConstructor;
Error.prototype = env.internals.err ?? {};
Error.prototype.name = 'Error';
setConstr(Error.prototype, Error, env);
Error.prototype.toString = function() {
if (!(this instanceof Error)) return '';
if (this.message === '') return this.name;
else return this.name + ': ' + this.message;
};
setConstr(Error.prototype, Error);
setProps(Error.prototype, {
name: 'Error',
toString: internals.setEnv(function(this: Error) {
if (!(this instanceof Error)) return '';
if (this.message === '') return this.name;
else return this.name + ': ' + this.message;
}, env)
});
env.setProto('error', Error.prototype);
internals.markSpecial(Error);
function makeError<T extends ErrorConstructor>(name: string, proto: any): T {
var err = function (msg: string) {
function makeError<T1 extends ErrorConstructor>(name: string, proto: string): T1 {
function constr (msg: string) {
var res = new Error(msg);
(res as any).__proto__ = err.prototype;
(res as any).__proto__ = constr.prototype;
return res;
} as T;
}
err.prototype = proto;
err.prototype.name = name;
setConstr(err.prototype, err as ErrorConstructor, env);
(err.prototype as any).__proto__ = Error.prototype;
(err as any).__proto__ = Error;
env.internals.special(err);
(constr as any).__proto__ = Error;
(constr.prototype as any).__proto__ = env.proto('error');
setConstr(constr.prototype, constr as ErrorConstructor);
setProps(constr.prototype, { name: name });
return err;
internals.markSpecial(constr);
env.setProto(proto, constr.prototype);
return constr as T1;
}
env.global.RangeError = makeError('RangeError', env.internals.range ?? {});
env.global.TypeError = makeError('TypeError', env.internals.type ?? {});
env.global.SyntaxError = makeError('SyntaxError', env.internals.syntax ?? {});
env.global.RangeError = makeError('RangeError', 'rangeErr');
env.global.TypeError = makeError('TypeError', 'typeErr');
env.global.SyntaxError = makeError('SyntaxError', 'syntaxErr');
});

View File

@@ -3,15 +3,15 @@ define("values/function", () => {
throw 'Using the constructor Function() is forbidden.';
} as unknown as FunctionConstructor;
Function.prototype = (Function as any).__proto__ as Function;
setConstr(Function.prototype, Function, env);
env.setProto('function', Function.prototype);
setConstr(Function.prototype, Function);
setProps(Function.prototype, env, {
setProps(Function.prototype, {
apply(thisArg, args) {
if (typeof args !== 'object') throw 'Expected arguments to be an array-like object.';
var len = args.length - 0;
let newArgs: any[];
if (Array.isArray(args)) newArgs = args;
if (internals.isArray(args)) newArgs = args;
else {
newArgs = [];
@@ -21,21 +21,20 @@ define("values/function", () => {
}
}
return env.internals.apply(this, thisArg, newArgs);
return internals.apply(this, thisArg, newArgs);
},
call(thisArg, ...args) {
return this.apply(thisArg, args);
},
bind(thisArg, ...args) {
var func = this;
const func = this;
const res = function() {
const resArgs = [];
var res = function() {
var resArgs = [];
for (var i = 0; i < args.length; i++) {
for (let i = 0; i < args.length; i++) {
resArgs[i] = args[i];
}
for (var i = 0; i < arguments.length; i++) {
for (let i = 0; i < arguments.length; i++) {
resArgs[i + args.length] = arguments[i];
}
@@ -48,7 +47,7 @@ define("values/function", () => {
return 'function (...) { ... }';
},
});
setProps(Function, env, {
setProps(Function, {
async(func) {
if (typeof func !== 'function') throw new TypeError('Expected func to be function.');
@@ -56,7 +55,7 @@ define("values/function", () => {
const args = arguments;
return new Promise((res, rej) => {
const gen = Function.generator(func as any).apply(this, args as any);
const gen = internals.apply(internals.generator(func as any), this, args as any);
(function next(type: 'none' | 'err' | 'ret', val?: any) {
try {
@@ -83,11 +82,11 @@ define("values/function", () => {
asyncGenerator(func) {
if (typeof func !== 'function') throw new TypeError('Expected func to be function.');
return function(this: any) {
const gen = Function.generator<any[], ['await' | 'yield', any]>((_yield) => func(
return function(this: any, ...args: any[]) {
const gen = internals.apply(internals.generator((_yield) => func(
val => _yield(['await', val]) as any,
val => _yield(['yield', val])
)).apply(this, arguments as any);
)), this, args) as Generator<['await' | 'yield', any]>;
const next = (resolve: Function, reject: Function, type: 'none' | 'val' | 'ret' | 'err', val?: any) => {
let res;
@@ -124,17 +123,18 @@ define("values/function", () => {
},
generator(func) {
if (typeof func !== 'function') throw new TypeError('Expected func to be function.');
const gen = env.internals.makeGenerator(func);
return (...args: any[]) => {
const it = gen(args);
const gen = internals.generator(func);
return function(this: any, ...args: any[]) {
const it = internals.apply(gen, this, args);
return {
next: it.next,
return: it.return,
throw: it.throw,
next: (...args) => internals.apply(it.next, it, args),
return: (val) => internals.apply(it.next, it, [val]),
throw: (val) => internals.apply(it.next, it, [val]),
[Symbol.iterator]() { return this; }
}
}
}
})
internals.markSpecial(Function);
});

View File

@@ -7,10 +7,10 @@ define("values/number", () => {
else (this as any).value = val;
} as NumberConstructor;
Number.prototype = (0 as any).__proto__ as Number;
setConstr(Number.prototype, Number, env);
env.setProto('number', Number.prototype);
setConstr(Number.prototype, Number);
setProps(Number.prototype, env, {
setProps(Number.prototype, {
valueOf() {
if (typeof this === 'number') return this;
else return (this as any).value;
@@ -21,13 +21,13 @@ define("values/number", () => {
}
});
setProps(Number, env, {
parseInt(val) { return Math.trunc(Number.parseFloat(val)); },
parseFloat(val) { return env.internals.parseFloat(val); },
setProps(Number, {
parseInt(val) { return Math.trunc(val as any - 0); },
parseFloat(val) { return val as any - 0; },
});
env.global.Object.defineProperty(env.global, 'parseInt', { value: Number.parseInt, writable: false });
env.global.Object.defineProperty(env.global, 'parseFloat', { value: Number.parseFloat, writable: false });
env.global.parseInt = Number.parseInt;
env.global.parseFloat = Number.parseFloat;
env.global.Object.defineProperty(env.global, 'NaN', { value: 0 / 0, writable: false });
env.global.Object.defineProperty(env.global, 'Infinity', { value: 1 / 0, writable: false });
});

View File

@@ -8,8 +8,9 @@ define("values/object", () => {
return arg;
} as ObjectConstructor;
Object.prototype = ({} as any).__proto__ as Object;
setConstr(Object.prototype, Object as any, env);
env.setProto('object', Object.prototype);
(Object.prototype as any).__proto__ = null;
setConstr(Object.prototype, Object as any);
function throwNotObject(obj: any, name: string) {
if (obj === null || typeof obj !== 'object' && typeof obj !== 'function') {
@@ -20,8 +21,8 @@ define("values/object", () => {
return typeof obj === 'object' && obj !== null || typeof obj === 'function';
}
setProps(Object, env, {
assign: function(dst, ...src) {
setProps(Object, {
assign(dst, ...src) {
throwNotObject(dst, 'assign');
for (let i = 0; i < src.length; i++) {
const obj = src[i];
@@ -40,10 +41,10 @@ define("values/object", () => {
defineProperty(obj, key, attrib) {
throwNotObject(obj, 'defineProperty');
if (typeof attrib !== 'object') throw new TypeError('Expected attributes to be an object.');
if ('value' in attrib) {
if ('get' in attrib || 'set' in attrib) throw new TypeError('Cannot specify a value and accessors for a property.');
if (!env.internals.defineField(
if (!internals.defineField(
obj, key,
attrib.value,
!!attrib.writable,
@@ -54,8 +55,8 @@ define("values/object", () => {
else {
if (typeof attrib.get !== 'function' && attrib.get !== undefined) throw new TypeError('Get accessor must be a function.');
if (typeof attrib.set !== 'function' && attrib.set !== undefined) throw new TypeError('Set accessor must be a function.');
if (!env.internals.defineProp(
if (!internals.defineProp(
obj, key,
attrib.get,
attrib.set,
@@ -63,50 +64,87 @@ define("values/object", () => {
!!attrib.configurable
)) throw new TypeError('Can\'t define property \'' + key + '\'.');
}
return obj;
},
defineProperties(obj, attrib) {
throwNotObject(obj, 'defineProperties');
if (typeof attrib !== 'object' && typeof attrib !== 'function') throw 'Expected second argument to be an object.';
for (var key in attrib) {
Object.defineProperty(obj, key, attrib[key]);
}
return obj;
},
keys(obj, onlyString) {
onlyString = !!(onlyString ?? true);
return env.internals.keys(obj, onlyString);
return internals.keys(obj, !!(onlyString ?? true));
},
entries(obj, onlyString) {
return Object.keys(obj, onlyString).map(v => [ v, (obj as any)[v] ]);
const res = [];
const keys = internals.keys(obj, !!(onlyString ?? true));
for (let i = 0; i < keys.length; i++) {
res[i] = [ keys[i], (obj as any)[keys[i]] ];
}
return keys;
},
values(obj, onlyString) {
return Object.keys(obj, onlyString).map(v => (obj as any)[v]);
const res = [];
const keys = internals.keys(obj, !!(onlyString ?? true));
for (let i = 0; i < keys.length; i++) {
res[i] = (obj as any)[keys[i]];
}
return keys;
},
getOwnPropertyDescriptor(obj, key) {
return env.internals.ownProp(obj, key);
return internals.ownProp(obj, key) as any;
},
getOwnPropertyDescriptors(obj) {
return Object.fromEntries([
...Object.getOwnPropertyNames(obj),
...Object.getOwnPropertySymbols(obj)
].map(v => [ v, Object.getOwnPropertyDescriptor(obj, v) ])) as any;
const res = [];
const keys = internals.ownPropKeys(obj);
for (let i = 0; i < keys.length; i++) {
res[i] = internals.ownProp(obj, keys[i]);
}
return res;
},
getOwnPropertyNames(obj) {
return env.internals.ownPropKeys(obj, false);
const arr = internals.ownPropKeys(obj);
const res = [];
for (let i = 0; i < arr.length; i++) {
if (typeof arr[i] === 'symbol') continue;
res[res.length] = arr[i];
}
return res as any;
},
getOwnPropertySymbols(obj) {
return env.internals.ownPropKeys(obj, true);
const arr = internals.ownPropKeys(obj);
const res = [];
for (let i = 0; i < arr.length; i++) {
if (typeof arr[i] !== 'symbol') continue;
res[res.length] = arr[i];
}
return res as any;
},
hasOwn(obj, key) {
if (Object.getOwnPropertyNames(obj).includes(key)) return true;
if (Object.getOwnPropertySymbols(obj).includes(key)) return true;
const keys = internals.ownPropKeys(obj);
for (let i = 0; i < keys.length; i++) {
if (keys[i] === key) return true;
}
return false;
},
@@ -130,41 +168,51 @@ define("values/object", () => {
preventExtensions(obj) {
throwNotObject(obj, 'preventExtensions');
env.internals.preventExtensions(obj);
internals.lock(obj, 'ext');
return obj;
},
seal(obj) {
throwNotObject(obj, 'seal');
env.internals.seal(obj);
internals.lock(obj, 'seal');
return obj;
},
freeze(obj) {
throwNotObject(obj, 'freeze');
env.internals.freeze(obj);
internals.lock(obj, 'freeze');
return obj;
},
isExtensible(obj) {
if (!check(obj)) return false;
return env.internals.extensible(obj);
return internals.extensible(obj);
},
isSealed(obj) {
if (!check(obj)) return true;
if (Object.isExtensible(obj)) return false;
return Object.getOwnPropertyNames(obj).every(v => !Object.getOwnPropertyDescriptor(obj, v).configurable);
if (internals.extensible(obj)) return false;
const keys = internals.ownPropKeys(obj);
for (let i = 0; i < keys.length; i++) {
if (internals.ownProp(obj, keys[i]).configurable) return false;
}
return true;
},
isFrozen(obj) {
if (!check(obj)) return true;
if (Object.isExtensible(obj)) return false;
return Object.getOwnPropertyNames(obj).every(v => {
var prop = Object.getOwnPropertyDescriptor(obj, v);
if (internals.extensible(obj)) return false;
const keys = internals.ownPropKeys(obj);
for (let i = 0; i < keys.length; i++) {
const prop = internals.ownProp(obj, keys[i]);
if (prop.configurable) return false;
if ('writable' in prop && prop.writable) return false;
return !prop.configurable;
});
}
return true;
}
});
setProps(Object.prototype, env, {
setProps(Object.prototype, {
valueOf() {
return this;
},
@@ -175,4 +223,5 @@ define("values/object", () => {
return Object.hasOwn(this, key);
},
});
internals.markSpecial(Object);
});

View File

@@ -7,10 +7,10 @@ define("values/string", () => {
else (this as any).value = val;
} as StringConstructor;
String.prototype = ('' as any).__proto__ as String;
setConstr(String.prototype, String, env);
env.setProto('string', String.prototype);
setConstr(String.prototype, String);
setProps(String.prototype, env, {
setProps(String.prototype, {
toString() {
if (typeof this === 'string') return this;
else return (this as any).value;
@@ -27,7 +27,14 @@ define("values/string", () => {
}
start = start ?? 0 | 0;
end = (end ?? this.length) | 0;
return env.internals.substring(this, start, end);
const res = [];
for (let i = start; i < end; i++) {
if (i >= 0 && i < this.length) res[res.length] = this[i];
}
return internals.stringFromStrings(res);
},
substr(start, length) {
start = start ?? 0 | 0;
@@ -36,14 +43,41 @@ define("values/string", () => {
if (start < 0) start = 0;
length = (length ?? this.length - start) | 0;
return this.substring(start, length + start);
const end = length + start;
const res = [];
for (let i = start; i < end; i++) {
if (i >= 0 && i < this.length) res[res.length] = this[i];
}
return internals.stringFromStrings(res);
},
toLowerCase() {
return env.internals.toLower(this + '');
// TODO: Implement localization
const res = [];
for (let i = 0; i < this.length; i++) {
const c = internals.char(this[i]);
if (c >= 65 && c <= 90) res[i] = c - 65 + 97;
else res[i] = c;
}
return internals.stringFromChars(res);
},
toUpperCase() {
return env.internals.toUpper(this + '');
// TODO: Implement localization
const res = [];
for (let i = 0; i < this.length; i++) {
const c = internals.char(this[i]);
if (c >= 97 && c <= 122) res[i] = c - 97 + 65;
else res[i] = c;
}
return internals.stringFromChars(res);
},
charAt(pos) {
@@ -57,9 +91,14 @@ define("values/string", () => {
return this[pos];
},
charCodeAt(pos) {
var res = this.charAt(pos);
if (res === '') return NaN;
else return env.internals.toCharCode(res);
if (typeof this !== 'string') {
if (this instanceof String) return (this as any).value.charAt(pos);
else throw new Error('This function may be used only with primitive or object strings.');
}
pos = pos | 0;
if (pos < 0 || pos >= this.length) return 0 / 0;
return internals.char(this[pos]);
},
startsWith(term, pos) {
@@ -68,7 +107,15 @@ define("values/string", () => {
else throw new Error('This function may be used only with primitive or object strings.');
}
pos = pos! | 0;
return env.internals.startsWith(this, term + '', pos);
term = term + "";
if (pos < 0 || this.length < term.length + pos) return false;
for (let i = 0; i < term.length; i++) {
if (this[i + pos] !== term[i]) return false;
}
return true;
},
endsWith(term, pos) {
if (typeof this !== 'string') {
@@ -76,7 +123,17 @@ define("values/string", () => {
else throw new Error('This function may be used only with primitive or object strings.');
}
pos = (pos ?? this.length) | 0;
return env.internals.endsWith(this, term + '', pos);
term = term + "";
const start = pos - term.length;
if (start < 0 || this.length < term.length + start) return false;
for (let i = 0; i < term.length; i++) {
if (this[i + start] !== term[i]) return false;
}
return true;
},
indexOf(term: any, start) {
@@ -189,9 +246,9 @@ define("values/string", () => {
}
});
setProps(String, env, {
setProps(String, {
fromCharCode(val) {
return env.internals.fromCharCode(val | 0);
return internals.stringFromChars([val | 0]);
},
})
@@ -202,7 +259,7 @@ define("values/string", () => {
else throw new Error('This function may be used only with primitive or object strings.');
}
return env.internals.strlen(this);
return internals.strlen(this);
},
configurable: true,
enumerable: false,

View File

@@ -1,31 +1,34 @@
define("values/symbol", () => {
const symbols: Record<string, symbol> = { };
var Symbol = env.global.Symbol = function(this: any, val?: string) {
if (this !== undefined && this !== null) throw new TypeError("Symbol may not be called with 'new'.");
if (typeof val !== 'string' && val !== undefined) throw new TypeError('val must be a string or undefined.');
return env.internals.symbol(val, true);
return internals.symbol(val);
} as SymbolConstructor;
Symbol.prototype = env.internals.symbolProto;
setConstr(Symbol.prototype, Symbol, env);
(Symbol as any).typeName = Symbol("Symbol.name");
(Symbol as any).replace = Symbol('Symbol.replace');
(Symbol as any).match = Symbol('Symbol.match');
(Symbol as any).matchAll = Symbol('Symbol.matchAll');
(Symbol as any).split = Symbol('Symbol.split');
(Symbol as any).search = Symbol('Symbol.search');
(Symbol as any).iterator = Symbol('Symbol.iterator');
(Symbol as any).asyncIterator = Symbol('Symbol.asyncIterator');
env.setProto('symbol', Symbol.prototype);
setConstr(Symbol.prototype, Symbol);
setProps(Symbol, env, {
setProps(Symbol, {
for(key) {
if (typeof key !== 'string' && key !== undefined) throw new TypeError('key must be a string or undefined.');
return env.internals.symbol(key, false);
if (key in symbols) return symbols[key];
else return symbols[key] = internals.symbol(key);
},
keyFor(sym) {
if (typeof sym !== 'symbol') throw new TypeError('sym must be a symbol.');
return env.internals.symStr(sym);
return internals.symbolToString(sym);
},
typeName: Symbol('Symbol.name') as any,
typeName: Symbol("Symbol.name") as any,
replace: Symbol('Symbol.replace') as any,
match: Symbol('Symbol.match') as any,
matchAll: Symbol('Symbol.matchAll') as any,
split: Symbol('Symbol.split') as any,
search: Symbol('Symbol.search') as any,
iterator: Symbol('Symbol.iterator') as any,
asyncIterator: Symbol('Symbol.asyncIterator') as any,
});
env.global.Object.defineProperty(Object.prototype, Symbol.typeName, { value: 'Object' });