Skip to content

Commit 070ff8d

Browse files
committed
replace predefined globals with global()
1 parent ecfca97 commit 070ff8d

4 files changed

Lines changed: 21 additions & 43 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ In JavaScript code:
1818
```javascript
1919
import {WindowsScriptingHost} from '@arcticnotes/node-wsh';
2020

21-
const wsh = await WindowsScriptingHost.connect();
22-
const WScript = wsh.WScript;
21+
const WSH = await WindowsScriptingHost.connect();
22+
const WScript = WSH.global( 'WScript');
2323
console.log(WScript.Version);
24-
await wsh.disconnect();
24+
await WSH.disconnect();
2525
```
2626

2727
## Dependencies

lib/node/node-wsh.js

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { Syncline} from "@arcticnotes/syncline";
55
const COMMAND = 'cscript.exe';
66
const ARGS = [ '//E:jscript', '//NoLogo'];
77
const SCRIPT_FILE = PATH.join( PATH.dirname( import.meta.dirname), 'wsh', 'host.js');
8-
const TRACE_REF = 1;
98

109
export class WindowsScriptingHost extends EventEmitter {
1110

@@ -14,11 +13,10 @@ export class WindowsScriptingHost extends EventEmitter {
1413
const args = options.args || ARGS;
1514
const scriptFile = options.scriptFile || SCRIPT_FILE;
1615
const trace = options.trace || 0;
17-
return new WindowsScriptingHost( await Syncline.spawn( command, [ ...args, scriptFile], { trace}), options);
16+
return new WindowsScriptingHost( await Syncline.spawn( command, [ ...args, scriptFile], { trace}));
1817
}
1918

2019
#syncline;
21-
#trace;
2220
#finalizer = new FinalizationRegistry( this.#finalized.bind( this));
2321
#ref2proxy = new Map();
2422
#proxy2ref = new WeakMap();
@@ -87,19 +85,11 @@ export class WindowsScriptingHost extends EventEmitter {
8785
},
8886
};
8987

90-
#WScript;
91-
#GetObject;
92-
#Enumerator;
93-
94-
constructor( syncline, options) {
88+
constructor( syncline) {
9589
super();
9690
this.#syncline = syncline;
9791
this.#syncline.on( 'stderr', line => console.log( 'wsh:', line));
9892
this.#syncline.on( 'stdout', line => console.log( 'wsh:', line));
99-
this.#trace = options.trace;
100-
this.#WScript = this.#getOrCreateObject( 0);
101-
this.#GetObject = this.#getOrCreateFunction( 1);
102-
this.#Enumerator = this.#getOrCreateFunction( 2);
10393
}
10494

10595
get remoteObjects() {
@@ -111,16 +101,13 @@ export class WindowsScriptingHost extends EventEmitter {
111101
};
112102
}
113103

114-
get WScript() {
115-
return this.#WScript;
116-
}
117-
118-
get GetObject() {
119-
return this.#GetObject;
120-
}
121-
122-
get Enumerator() {
123-
return this.#Enumerator;
104+
global( name) {
105+
const output = JSON.parse( this.#syncline.exchange( JSON.stringify( [ 'global', name])));
106+
switch( output[ 0]) {
107+
case 'value': return this.#decode( output[ 1]);
108+
case 'error': throw new Error( output[ 1]);
109+
default: throw new Error( `unknown status: ${ output[ 0]}`);
110+
}
124111
}
125112

126113
async disconnect() {
@@ -155,17 +142,6 @@ export class WindowsScriptingHost extends EventEmitter {
155142
if( this.#ref2proxy.get( ref).deref() === undefined) // otherwise, it's overwritten by a refreshed proxy
156143
this.#ref2proxy.delete( ref);
157144
const output = this.#syncline.exchange( JSON.stringify( [ 'unref', ref]));
158-
if( this.#trace >= TRACE_REF)
159-
switch( output[ 0]) {
160-
case 'error':
161-
console.log( `failed to unref: ${ ref}`);
162-
break;
163-
case 'done':
164-
console.log( `unreferenced: ${ ref}`);
165-
break;
166-
default:
167-
console.log( `unknown response: ${ output[ 0]}`);
168-
}
169145
this.emit( 'unref', ref);
170146
}
171147

lib/wsh/host.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
var FSO = new ActiveXObject( 'Scripting.FileSystemObject');
44
var OBJECT_TOSTRING = Object.toString();
5-
var REFERENCES = { // must match node-wsh.js
6-
'0': { type: 'obj', value: WScript},
7-
'1': { type: 'fun', value: GetObject},
8-
'2': { type: 'fun', value: Enumerator}
9-
};
10-
var nextRefId = 3;
5+
var GLOBAL = this;
6+
var REFERENCES = {}
7+
var nextRefId = 0;
118

129
eval( FSO.OpenTextFile( FSO.BuildPath( FSO.GetParentFolderName( WScript.ScriptFullName), 'json2.js')).ReadAll());
1310

@@ -145,6 +142,9 @@ function decodePotentialMethod( encoded) {
145142
try {
146143
input = JSON.parse( WScript.StdIn.ReadLine());
147144
switch( input[ 0]) {
145+
case 'global': // [ 'global', name] => [ 'value', value]
146+
output = [ 'value', encode( GLOBAL[ input[ 1]])];
147+
break;
148148
case 'unref': // [ 'unref', ref] => [ 'done']
149149
if( REFERENCES[ input[ 1]] === undefined)
150150
throw new Error( 'unknown ref: ' + input[ 1]);

test/test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ TEST( 'smoke-test', async() => {
77
wsh.on( 'ref', ( ref, obj) => console.log( 'ref', ref, obj));
88
wsh.on( 'unref', ref => console.log( 'unref', ref));
99
try {
10-
const { WScript, GetObject, Enumerator} = wsh;
10+
const WScript = wsh.global( 'WScript');
11+
const GetObject = wsh.global( 'GetObject');
12+
const Enumerator = wsh.global( 'Enumerator');
1113
console.log( WScript.Version);
1214
ASSERT.equal( typeof WScript.Version, 'string');
1315
const procs = GetObject( "winmgmts:\\\\.\\root\\cimv2").ExecQuery( 'SELECT ProcessId, Name FROM Win32_Process');

0 commit comments

Comments
 (0)