feat: implement map and set polyfills in java

This commit is contained in:
2023-09-26 08:31:27 +03:00
parent cf36b7adc5
commit e16c0fedb1
21 changed files with 208 additions and 201 deletions

View File

@@ -12,6 +12,8 @@ interface Internals {
bool: BooleanConstructor;
number: NumberConstructor;
string: StringConstructor;
map: typeof Map;
set: typeof Set;
markSpecial(...funcs: Function[]): void;
getEnv(func: Function): Environment | undefined;
@@ -45,19 +47,23 @@ interface Internals {
try {
var env: Environment = arguments[0], internals: Internals = arguments[1];
var Object = env.global.Object = internals.object;
var Function = env.global.Function = internals.function;
var Array = env.global.Array = internals.array;
var Promise = env.global.Promise = internals.promise;
var Boolean = env.global.Boolean = internals.bool;
var Number = env.global.Number = internals.number;
var String = env.global.String = internals.string;
const Object = env.global.Object = internals.object;
const Function = env.global.Function = internals.function;
const Array = env.global.Array = internals.array;
const Promise = env.global.Promise = internals.promise;
const Boolean = env.global.Boolean = internals.bool;
const Number = env.global.Number = internals.number;
const String = env.global.String = internals.string;
const Map = env.global.Map = internals.map;
const Set = env.global.Set = internals.set;
env.setProto('object', Object.prototype);
env.setProto('function', Function.prototype);
env.setProto('array', Array.prototype);
env.setProto('number', Number.prototype);
env.setProto('string', String.prototype);
env.setProto('bool', Boolean.prototype);
(Object.prototype as any).__proto__ = null;
@@ -66,8 +72,6 @@ try {
run('values/symbol');
run('values/errors');
run('map');
run('set');
run('regex');
run('timeout');

View File

@@ -1,93 +0,0 @@
define("map", () => {
const syms = { values: internals.symbol('Map.values') } as { readonly values: unique symbol };
const Object = env.global.Object;
class Map<KeyT, ValueT> {
[syms.values]: any = {};
public [env.global.Symbol.iterator](): IterableIterator<[KeyT, ValueT]> {
return this.entries();
}
public clear() {
this[syms.values] = {};
}
public delete(key: KeyT) {
if ((key as any) in this[syms.values]) {
delete this[syms.values];
return true;
}
else return false;
}
public entries(): IterableIterator<[KeyT, ValueT]> {
const keys = internals.ownPropKeys(this[syms.values]);
let i = 0;
return {
next: () => {
if (i >= keys.length) return { done: true };
else return { done: false, value: [ keys[i], this[syms.values][keys[i++]] ] }
},
[env.global.Symbol.iterator]() { return this; }
}
}
public keys(): IterableIterator<KeyT> {
const keys = internals.ownPropKeys(this[syms.values]);
let i = 0;
return {
next: () => {
if (i >= keys.length) return { done: true };
else return { done: false, value: keys[i] }
},
[env.global.Symbol.iterator]() { return this; }
}
}
public values(): IterableIterator<ValueT> {
const keys = internals.ownPropKeys(this[syms.values]);
let i = 0;
return {
next: () => {
if (i >= keys.length) return { done: true };
else return { done: false, value: this[syms.values][keys[i++]] }
},
[env.global.Symbol.iterator]() { return this; }
}
}
public get(key: KeyT) {
return this[syms.values][key];
}
public set(key: KeyT, val: ValueT) {
this[syms.values][key] = val;
return this;
}
public has(key: KeyT) {
return (key as any) in this[syms.values][key];
}
public get size() {
return internals.ownPropKeys(this[syms.values]).length;
}
public forEach(func: (key: KeyT, val: ValueT, map: Map<KeyT, ValueT>) => void, thisArg?: any) {
const keys = internals.ownPropKeys(this[syms.values]);
for (let i = 0; i < keys.length; i++) {
func(keys[i], this[syms.values][keys[i]], this);
}
}
public constructor(iterable: Iterable<[KeyT, ValueT]>) {
const it = iterable[env.global.Symbol.iterator]();
for (let el = it.next(); !el.done; el = it.next()) {
this[syms.values][el.value[0]] = el.value[1];
}
}
}
env.global.Map = Map;
});

View File

@@ -1,81 +0,0 @@
define("set", () => {
const syms = { values: internals.symbol('Map.values') } as { readonly values: unique symbol };
const Object = env.global.Object;
class Set<T> {
[syms.values]: any = {};
public [env.global.Symbol.iterator](): IterableIterator<[T, T]> {
return this.entries();
}
public clear() {
this[syms.values] = {};
}
public delete(key: T) {
if ((key as any) in this[syms.values]) {
delete this[syms.values];
return true;
}
else return false;
}
public entries(): IterableIterator<[T, T]> {
const keys = internals.ownPropKeys(this[syms.values]);
let i = 0;
return {
next: () => {
if (i >= keys.length) return { done: true };
else return { done: false, value: [ keys[i], keys[i] ] }
},
[env.global.Symbol.iterator]() { return this; }
}
}
public keys(): IterableIterator<T> {
const keys = internals.ownPropKeys(this[syms.values]);
let i = 0;
return {
next: () => {
if (i >= keys.length) return { done: true };
else return { done: false, value: keys[i] }
},
[env.global.Symbol.iterator]() { return this; }
}
}
public values(): IterableIterator<T> {
return this.keys();
}
public add(val: T) {
this[syms.values][val] = undefined;
return this;
}
public has(key: T) {
return (key as any) in this[syms.values][key];
}
public get size() {
return internals.ownPropKeys(this[syms.values]).length;
}
public forEach(func: (key: T, val: T, map: Set<T>) => void, thisArg?: any) {
const keys = internals.ownPropKeys(this[syms.values]);
for (let i = 0; i < keys.length; i++) {
func(keys[i], this[syms.values][keys[i]], this);
}
}
public constructor(iterable: Iterable<T>) {
const it = iterable[env.global.Symbol.iterator]();
for (let el = it.next(); !el.done; el = it.next()) {
this[syms.values][el.value] = undefined;
}
}
}
env.global.Set = Set;
});

View File

@@ -31,6 +31,6 @@ define("values/symbol", () => {
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);
// internals.defineField(env.global.Object.prototype, Symbol.typeName, 'Object', false, false, false);
// internals.defineField(env.global, Symbol.typeName, 'Window', false, false, false);
});