feat: implement symbols in java
This commit is contained in:
@@ -12,6 +12,8 @@ interface Internals {
|
||||
bool: BooleanConstructor;
|
||||
number: NumberConstructor;
|
||||
string: StringConstructor;
|
||||
symbol: SymbolConstructor;
|
||||
|
||||
map: typeof Map;
|
||||
set: typeof Set;
|
||||
|
||||
@@ -26,7 +28,7 @@ interface Internals {
|
||||
char(val: string): number;
|
||||
stringFromStrings(arr: string[]): string;
|
||||
stringFromChars(arr: number[]): string;
|
||||
symbol(name?: string): symbol;
|
||||
getSymbol(name?: string): symbol;
|
||||
symbolToString(sym: symbol): string;
|
||||
|
||||
isArray(obj: any): boolean;
|
||||
@@ -54,6 +56,7 @@ try {
|
||||
const Boolean = env.global.Boolean = internals.bool;
|
||||
const Number = env.global.Number = internals.number;
|
||||
const String = env.global.String = internals.string;
|
||||
const Symbol = env.global.Symbol = internals.symbol;
|
||||
|
||||
const Map = env.global.Map = internals.map;
|
||||
const Set = env.global.Set = internals.set;
|
||||
@@ -63,6 +66,7 @@ try {
|
||||
env.setProto('array', Array.prototype);
|
||||
env.setProto('number', Number.prototype);
|
||||
env.setProto('string', String.prototype);
|
||||
env.setProto('symbol', Symbol.prototype);
|
||||
env.setProto('bool', Boolean.prototype);
|
||||
|
||||
(Object.prototype as any).__proto__ = null;
|
||||
@@ -70,7 +74,6 @@ try {
|
||||
internals.getEnv(run)?.setProto('array', Array.prototype);
|
||||
globalThis.log = (...args) => internals.apply(internals.log, internals, args);
|
||||
|
||||
run('values/symbol');
|
||||
run('values/errors');
|
||||
run('regex');
|
||||
run('timeout');
|
||||
|
||||
@@ -3,10 +3,7 @@
|
||||
"lib.d.ts",
|
||||
"modules.ts",
|
||||
"utils.ts",
|
||||
"values/symbol.ts",
|
||||
"values/errors.ts",
|
||||
"map.ts",
|
||||
"set.ts",
|
||||
"regex.ts",
|
||||
"timeout.ts",
|
||||
"core.ts"
|
||||
|
||||
11
lib/utils.ts
11
lib/utils.ts
@@ -25,14 +25,3 @@ function setConstr(target: object, constr: Function) {
|
||||
true // configurable
|
||||
);
|
||||
}
|
||||
|
||||
function wrapI(max: number, i: number) {
|
||||
i |= 0;
|
||||
if (i < 0) i = max + i;
|
||||
return i;
|
||||
}
|
||||
function clampI(max: number, i: number) {
|
||||
if (i < 0) i = 0;
|
||||
if (i > max) i = max;
|
||||
return i;
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
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 env.global.TypeError("Symbol may not be called with 'new'.");
|
||||
if (typeof val !== 'string' && val !== undefined) throw new env.global.TypeError('val must be a string or undefined.');
|
||||
return internals.symbol(val);
|
||||
} as SymbolConstructor;
|
||||
|
||||
env.setProto('symbol', Symbol.prototype);
|
||||
setConstr(Symbol.prototype, Symbol);
|
||||
|
||||
setProps(Symbol, {
|
||||
for(key) {
|
||||
if (typeof key !== 'string' && key !== undefined) throw new env.global.TypeError('key must be a string or undefined.');
|
||||
if (key in symbols) return symbols[key];
|
||||
else return symbols[key] = internals.symbol(key);
|
||||
},
|
||||
keyFor(sym) {
|
||||
if (typeof sym !== 'symbol') throw new env.global.TypeError('sym must be a symbol.');
|
||||
return internals.symbolToString(sym);
|
||||
},
|
||||
|
||||
typeName: env.symbol("Symbol.typeName") as any,
|
||||
replace: env.symbol('Symbol.replace') as any,
|
||||
match: env.symbol('Symbol.match') as any,
|
||||
matchAll: env.symbol('Symbol.matchAll') as any,
|
||||
split: env.symbol('Symbol.split') as any,
|
||||
search: env.symbol('Symbol.search') as any,
|
||||
iterator: env.symbol('Symbol.iterator') as any,
|
||||
asyncIterator: env.symbol('Symbol.asyncIterator') as any,
|
||||
});
|
||||
|
||||
// internals.defineField(env.global.Object.prototype, Symbol.typeName, 'Object', false, false, false);
|
||||
// internals.defineField(env.global, Symbol.typeName, 'Window', false, false, false);
|
||||
});
|
||||
Reference in New Issue
Block a user