forked from Java2ArkTS/Java2ArkTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadBridge.ts
More file actions
234 lines (212 loc) · 5.54 KB
/
Copy pathThreadBridge.ts
File metadata and controls
234 lines (212 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
function booleanDecoder(sharedArray : Uint8Array){
if(sharedArray[0]===0){
return false;
}
return true;
}
function stringDecoder(sharedArray : Uint8Array) : string{
let receivedString = "";
for(let i = 0;i<sharedArray.length && sharedArray[i] !== 0;i++){
receivedString += String.fromCharCode(sharedArray[i]);
}
return receivedString;
}
export function getValues(code: any){
let type = code.sharedType;
if(type === "number"){
return code.sharedValue[0];
}
else if(type === "string"){
return stringDecoder(code.sharedValue);
}
else if(type === "boolean"){
return booleanDecoder(code.sharedValue);
}
else if(type === "bigint"){
return code.sharedValue[0];
}
else if(type === "undefined"){
return undefined;
}
else {
const members = Object.getOwnPropertyNames(code);
let result = {};
for(let i :number = 0;i<members.length;i++){
if(members[i]!=="sharedType") {
// result[members[i]] = getValues(code[members[i]]);
}
}
return result;
}
}
function booleanChanger(sharedArray : any, b : boolean){
sharedArray[0] = 0;
if(b === true){
sharedArray[0] = 1;
}
}
function stringChanger(sharedArray: any, s: string){
for(let i=0;i<s.length;i++){
sharedArray[i] = s.charCodeAt(i);
}
sharedArray[s.length] = 0;
}
export function setValues(obj: any, tmp: any){
let type = obj.sharedType;
if(type === "number"){
obj.sharedValue[0] = tmp;
}
else if(type === "string"){
stringChanger(obj.sharedValue, tmp);
}
else if(type === "boolean"){
booleanChanger(obj.sharedValue, tmp);
}
else if(type === "bigint"){
obj.sharedValue[0] = tmp;
}
else if(type === "undefined"){
return ;
}
else if(type === "Function"){
return ;
}
else {
const members = Object.getOwnPropertyNames(obj);
for(let i :number = 0;i<members.length;i++){
if(members[i]!=="sharedType"&&typeof obj[members[i]] != 'function') {
setValues(obj[members[i]], tmp[members[i]]);
}
}
}
}
function numberEncoder(n ?: number){
const buffer = new SharedArrayBuffer(8);
const sharedArray = new Float64Array(buffer);
sharedArray[0] = 0;
if(n!==null){
// sharedArray[0] = n;
}
return {"sharedValue": sharedArray, "sharedType": "number"};
}
function bigIntEncoder(n ?: bigint){
const buffer = new SharedArrayBuffer(8);
const sharedArray = new BigInt64Array(buffer);
// if(n!==null){
// sharedArray[0] = n;
// }
return {"sharedValue": sharedArray, "sharedType": "number"};
}
function booleanEncoder(b ?: boolean){
const buffer = new SharedArrayBuffer(1);
const sharedArray = new Uint8Array(buffer);
sharedArray[0] = 0;
if(b!==null){
if(b === true){
sharedArray[0] = 1;
}
}
return {"sharedValue": sharedArray, "sharedType": "boolean"};
}
function stringEncoder(s ?: string){
let long : number = 0;
if(s!==null){
// long = s.length + 3;
}
else{
long = 1024;
}
const buffer = new SharedArrayBuffer(long);
const sharedArray = new Uint8Array(buffer);
if(s!==null) {
// for(let i=0;i<s.length;i++){
// sharedArray[i] = s.charCodeAt(i);
// }
// sharedArray[s.length] = 0;
}
return {"sharedValue": sharedArray, "sharedType": "string"};
}
export function getClass(type: string, inClass ?: any){
if(type === 'number'){
return numberEncoder(inClass);
}
else if(type === 'boolean'){
return booleanEncoder(inClass);
}
else if(type === 'string') {
return stringEncoder(inClass);
}
else if(type === 'bigint') {
return bigIntEncoder(inClass);
}
else if(type === 'undefined'){
return {"sharedValue": undefined, "sharedType": "undefined"};
}
else{
return inClass;
}
}
export function getSyc(){
let syn = new SharedArrayBuffer(200);
let synArray = new Int8Array(syn);
for(let i=0;i<180;i++){
synArray[i] = 1;
}
return synArray;
}
export interface Runnable{
run(): void;
}
export class Thread{
static runnableList : any = [];
private runnableShared : any = null;
static runnableNum : number = 0;
private threadId : number = 0;
static getThreadOwn(myClass : any) : any{
return myClass;
}
public start(): void{
// taskpool.execute(threadStart, this.runnableShared, this.threadId);
}
constructor(runnableShared: any = null) {
if(runnableShared !== null){
this.runnableShared = runnableShared;
}
else{
this.runnableShared = Thread.getThreadOwn(this);
}
this.threadId = Thread.runnableNum;
Thread.runnableNum++;
Thread.runnableList.push(this.runnableShared);
}
}
export function addFunc(runnableShared: any, archetype: any){
if(runnableShared.sharedType === 'object'){
const members = Object.getOwnPropertyNames(runnableShared);
for(let i :number = 0;i<members.length;i++){
if(members[i]!=="sharedType") {
addFunc(runnableShared[members[i]], archetype[members[i]]);
}
}
const methodNames = Object.getOwnPropertyNames(archetype.__proto__).filter(
name => typeof (archetype.__proto__ as any)[name] === "function"
);
for(let i:number = 0;i<methodNames.length;i++){
runnableShared[methodNames[i]] = archetype.__proto__[methodNames[i]];
}
}
return;
}
// @Concurrent
function threadStart(runnableShared: any, threadId: number){
// sharedWash(runnableShared, threadId);
}
export function SynStart(i : number, synArray: Int8Array){
let lockState:number=0;
do {
lockState = Atomics.exchange(synArray, i, 0);
} while (lockState !== 1);
}
export function SynEnd(i : number, synArray: Int8Array){
Atomics.exchange(synArray, i, 1);
}