Skip to content

Commit 52152d7

Browse files
committed
merge WindowsScriptingHost and Proxies
1 parent b9fdfc3 commit 52152d7

2 files changed

Lines changed: 62 additions & 77 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ on:
33
push:
44
branches:
55
- master
6+
- dev
67
jobs:
78
build:
89
runs-on: windows-latest

lib/node/node-wsh.js

Lines changed: 61 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -18,80 +18,34 @@ export class WindowsScriptingHost extends EventEmitter {
1818
return new WindowsScriptingHost( await Syncline.spawn( command, [ ...args, scriptFile], { trace}), options);
1919
}
2020

21-
#syncline;
22-
#proxies;
23-
#WScript;
24-
#GetObject;
25-
#Enumerator;
26-
27-
constructor( syncline, options) {
28-
super();
29-
this.#syncline = syncline;
30-
this.#syncline.on( 'stderr', line => console.log( 'wsh:', line));
31-
this.#syncline.on( 'stdout', line => console.log( 'wsh:', line));
32-
this.#proxies = new Proxies( syncline, options, this);
33-
this.#WScript = this.#proxies.getOrCreateObject( 0);
34-
this.#GetObject = this.#proxies.getOrCreateFunction( 1);
35-
this.#Enumerator = this.#proxies.getOrCreateFunction( 2);
36-
}
37-
38-
get remoteObjects() {
39-
const proxies = this.#proxies;
40-
return {
41-
get count() {
42-
return proxies.count;
43-
},
44-
};
45-
}
46-
47-
get WScript() {
48-
return this.#WScript;
49-
}
50-
51-
get GetObject() {
52-
return this.#GetObject;
53-
}
54-
55-
get Enumerator() {
56-
return this.#Enumerator;
57-
}
58-
59-
async disconnect() {
60-
await this.#syncline.close();
61-
}
62-
}
63-
64-
class Proxies {
65-
6621
#syncline;
6722
#trace;
68-
#eventEmitter;
6923
#finalizer = new FinalizationRegistry( this.#finalized.bind( this));
7024
#ref2proxy = new Map();
7125
#proxy2ref = new Map();
7226

7327
#objectHandler = {
7428

75-
proxies: this,
29+
wsh: this,
7630

7731
get( target, prop) {
7832
if( prop === Symbol.toPrimitive)
79-
return () => `ref#${ this.proxies.#proxy2ref.get( target[ PROXY])}`;
80-
const encodedTarget = this.proxies.#encode( target[ PROXY]);
81-
const encodedProp = this.proxies.#encode( prop);
82-
const output = JSON.parse( this.proxies.#syncline.exchange( JSON.stringify( [ 'get', encodedTarget, encodedProp])));
33+
return () => `ref#${ this.wsh.#proxy2ref.get( target[ PROXY])}`;
34+
const encodedTarget = this.wsh.#encode( target[ PROXY]);
35+
const encodedProp = this.wsh.#encode( prop);
36+
const output = JSON.parse( this.wsh.#syncline.exchange( JSON.stringify( [ 'get', encodedTarget, encodedProp])));
8337
switch( output[ 0]) {
84-
case 'value': return this.proxies.#decode( output[ 1]);
38+
case 'value': return this.wsh.#decode( output[ 1]);
8539
case 'error': throw new Error( output[ 1]);
8640
default: throw new Error( `unknown status: ${ output[ 0]}`);
8741
}
8842
},
8943

9044
set( target, prop, value) {
91-
const encodedTarget = this.proxies.#encode( target[ PROXY]);
92-
const encodedProp = this.proxies.#encode( prop);
93-
const encodedValue = this.proxies.#encode( value);
94-
const output = JSON.parse( this.proxies.#syncline.exchange( JSON.stringify( [ 'set', encodedTarget, encodedProp, encodedValue])));
45+
const encodedTarget = this.wsh.#encode( target[ PROXY]);
46+
const encodedProp = this.wsh.#encode( prop);
47+
const encodedValue = this.wsh.#encode( value);
48+
const output = JSON.parse( this.wsh.#syncline.exchange( JSON.stringify( [ 'set', encodedTarget, encodedProp, encodedValue])));
9549
switch( output[ 0]) {
9650
case 'set': return;
9751
case 'error': throw new Error( output[ 1]);
@@ -102,53 +56,83 @@ class Proxies {
10256

10357
#functionHandler = {
10458

105-
proxies: this,
59+
wsh: this,
10660

10761
get( target, prop) {
10862
if( prop === Symbol.toPrimitive)
109-
return () => `ref#${ this.proxies.#proxy2ref.get( target[ PROXY])}`;
63+
return () => `ref#${ this.wsh.#proxy2ref.get( target[ PROXY])}`;
11064
return undefined;
11165
},
11266

11367
apply( target, thisArg, argumentList) {
114-
const encodedTarget = this.proxies.#encode( target[ PROXY]);
115-
const encodedThisArg = this.proxies.#encode( thisArg);
116-
const encodedArgumentList = this.proxies.#encode( argumentList);
117-
const output = JSON.parse( this.proxies.#syncline.exchange( JSON.stringify( [ 'apply', encodedTarget, encodedThisArg, encodedArgumentList])));
68+
const encodedTarget = this.wsh.#encode( target[ PROXY]);
69+
const encodedThisArg = this.wsh.#encode( thisArg);
70+
const encodedArgumentList = this.wsh.#encode( argumentList);
71+
const output = JSON.parse( this.wsh.#syncline.exchange( JSON.stringify( [ 'apply', encodedTarget, encodedThisArg, encodedArgumentList])));
11872
switch( output[ 0]) {
119-
case 'value': return this.proxies.#decode( output[ 1]);
73+
case 'value': return this.wsh.#decode( output[ 1]);
12074
case 'error': throw new Error( output[ 1]);
12175
default: throw new Error( `unknown status: ${ output[ 0]}`);
12276
}
12377
},
12478

12579
construct( target, argumentList) {
126-
const encodedTarget = this.proxies.#encode( target[ PROXY]);
127-
const encodedArgumentList = this.proxies.#encode( argumentList);
128-
const output = JSON.parse( this.proxies.#syncline.exchange( JSON.stringify( [ 'construct', encodedTarget, encodedArgumentList])));
80+
const encodedTarget = this.wsh.#encode( target[ PROXY]);
81+
const encodedArgumentList = this.wsh.#encode( argumentList);
82+
const output = JSON.parse( this.wsh.#syncline.exchange( JSON.stringify( [ 'construct', encodedTarget, encodedArgumentList])));
12983
switch( output[ 0]) {
130-
case 'value': return this.proxies.#decode( output[ 1]);
84+
case 'value': return this.wsh.#decode( output[ 1]);
13185
case 'error': throw new Error( output[ 1]);
13286
default: throw new Error( `unknown status: ${ output[ 0]}`);
13387
}
13488
},
13589
};
13690

137-
constructor( syncline, options, eventEmitter) {
91+
#WScript;
92+
#GetObject;
93+
#Enumerator;
94+
95+
constructor( syncline, options) {
96+
super();
13897
this.#syncline = syncline;
98+
this.#syncline.on( 'stderr', line => console.log( 'wsh:', line));
99+
this.#syncline.on( 'stdout', line => console.log( 'wsh:', line));
139100
this.#trace = options.trace;
140-
this.#eventEmitter = eventEmitter;
101+
this.#WScript = this.#getOrCreateObject( 0);
102+
this.#GetObject = this.#getOrCreateFunction( 1);
103+
this.#Enumerator = this.#getOrCreateFunction( 2);
141104
}
142105

143-
get count() {
144-
return this.#ref2proxy.size;
106+
get remoteObjects() {
107+
const proxies = this;
108+
return {
109+
get count() {
110+
return proxies.#ref2proxy.size;
111+
},
112+
};
113+
}
114+
115+
get WScript() {
116+
return this.#WScript;
117+
}
118+
119+
get GetObject() {
120+
return this.#GetObject;
121+
}
122+
123+
get Enumerator() {
124+
return this.#Enumerator;
125+
}
126+
127+
async disconnect() {
128+
await this.#syncline.close();
145129
}
146130

147-
getOrCreateObject( ref) {
131+
#getOrCreateObject( ref) {
148132
return this.#getOrCreate( ref, this.#objectHandler);
149133
}
150134

151-
getOrCreateFunction( ref) {
135+
#getOrCreateFunction( ref) {
152136
return this.#getOrCreate( ref, this.#functionHandler);
153137
}
154138

@@ -163,7 +147,7 @@ class Proxies {
163147
this.#ref2proxy.set( ref, newProxy);
164148
this.#proxy2ref.set( newProxy, ref);
165149
this.#finalizer.register( newProxy, ref);
166-
this.#eventEmitter.emit( 'ref', ref, newProxy);
150+
this.emit( 'ref', ref, newProxy);
167151
return newProxy;
168152
}
169153

@@ -180,7 +164,7 @@ class Proxies {
180164
default:
181165
console.log( `unknown response: ${ output[ 0]}`);
182166
}
183-
this.#eventEmitter.emit( 'unref', ref);
167+
this.emit( 'unref', ref);
184168
}
185169

186170
#encode( decoded) {
@@ -246,9 +230,9 @@ class Proxies {
246230
decoded[ name] = this.#decode( value);
247231
return decoded;
248232
case 'objref':
249-
return this.getOrCreateObject( encoded.value);
233+
return this.#getOrCreateObject( encoded.value);
250234
case 'funref':
251-
return this.getOrCreateFunction( encoded.value);
235+
return this.#getOrCreateFunction( encoded.value);
252236
default:
253237
throw new Error( `illegal value: ${ encoded}`);
254238
}

0 commit comments

Comments
 (0)