Skip to content

Commit

Permalink
tryfix #45
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieyang committed Aug 15, 2023
1 parent dd1f059 commit 2465a21
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions Javascripts~/PuertsDLLMock/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ export class JSObject {

export class jsFunctionOrObjectFactory {
private static regularID: number = 1;
private static freeID: number[] = [];
private static idMap = new WeakMap<Function | object, number>();
private static jsFuncOrObjectKV: { [id: number]: JSFunction | JSObject } = {};

Expand All @@ -256,21 +257,31 @@ export class jsFunctionOrObjectFactory {
return jsFunctionOrObjectFactory.jsFuncOrObjectKV[id] as JSFunction;
}

id = jsFunctionOrObjectFactory.regularID++;
if (this.freeID.length) {
id = this.freeID.pop();
} else {
id = jsFunctionOrObjectFactory.regularID++;
}

const func = new JSFunction(id, funcValue);
jsFunctionOrObjectFactory.idMap.set(funcValue, id);
jsFunctionOrObjectFactory.jsFuncOrObjectKV[id] = func;

return func;
}

public static getOrCreateJSObject(obj: object) {
public static getOrCreateJSObject(obj: object): JSObject {
let id = jsFunctionOrObjectFactory.idMap.get(obj);
if (id) {
return jsFunctionOrObjectFactory.jsFuncOrObjectKV[id];
return jsFunctionOrObjectFactory.jsFuncOrObjectKV[id] as JSObject;
}

id = jsFunctionOrObjectFactory.regularID++;
if (this.freeID.length) {
id = this.freeID.pop();
} else {
id = jsFunctionOrObjectFactory.regularID++;
}

const jsObject = new JSObject(id, obj);
jsFunctionOrObjectFactory.idMap.set(obj, id);
jsFunctionOrObjectFactory.jsFuncOrObjectKV[id] = jsObject;
Expand All @@ -284,8 +295,10 @@ export class jsFunctionOrObjectFactory {

public static removeJSObjectById(id: number): void {
const jsObject = jsFunctionOrObjectFactory.jsFuncOrObjectKV[id] as JSObject;
if (!jsObject) return console.warn('removeJSObjectById failed: id is invalid: ' + id);
jsFunctionOrObjectFactory.idMap.delete(jsObject.getObject());
delete jsFunctionOrObjectFactory.jsFuncOrObjectKV[id];
this.freeID.push(id);
}

public static getJSFunctionById(id: number): JSFunction {
Expand All @@ -294,8 +307,10 @@ export class jsFunctionOrObjectFactory {

public static removeJSFunctionById(id: number): void {
const jsFunc = jsFunctionOrObjectFactory.jsFuncOrObjectKV[id] as JSFunction;
if (!jsFunc) return console.warn('removeJSFunctionById failed: id is invalid: ' + id);
jsFunctionOrObjectFactory.idMap.delete(jsFunc._func);
delete jsFunctionOrObjectFactory.jsFuncOrObjectKV[id];
this.freeID.push(id);
}
}

Expand Down

0 comments on commit 2465a21

Please sign in to comment.