Skip to content

Commit de1f422

Browse files
committed
In JavaScriptEngineSwitcher.Node added a handling of errors that occur when switching to multi-process mode of the Jering.Javascript.NodeJS library
1 parent fe21ec6 commit de1f422

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

src/JavaScriptEngineSwitcher.Node/JavaScriptEngineSwitcher.Node.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
This package does not contain the `node.exe`. Therefore, you need to install the Node.js (https://nodejs.org) and add the `node.exe`'s directory to the `Path` environment variable (automatically done by the official installer).</Description>
1616
<PackageIcon>icon.png</PackageIcon>
1717
<PackageTags>JavaScriptEngineSwitcher;JavaScript;ECMAScript;Node.js;Jering.Javascript.NodeJS</PackageTags>
18-
<PackageReleaseNotes>Jering.Javascript.NodeJS was updated to version 5.4.0.</PackageReleaseNotes>
18+
<PackageReleaseNotes>Added a handling of errors that occur when switching to multi-process mode of the Jering.Javascript.NodeJS library.</PackageReleaseNotes>
1919
</PropertyGroup>
2020

2121
<Import Project="../../build/common.props" />

src/JavaScriptEngineSwitcher.Node/NodeJsEngine.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using WrapperRuntimeException = JavaScriptEngineSwitcher.Core.JsRuntimeException;
1919
using WrapperScriptException = JavaScriptEngineSwitcher.Core.JsScriptException;
2020
using WrapperTimeoutException = JavaScriptEngineSwitcher.Core.JsTimeoutException;
21+
using WrapperUsageException = JavaScriptEngineSwitcher.Core.JsUsageException;
2122

2223
using JavaScriptEngineSwitcher.Node.Helpers;
2324

@@ -286,6 +287,14 @@ private WrapperException WrapInvocationException(InvocationException originalExc
286287
wrapperScriptException = new WrapperCompilationException(message, EngineName, _engineVersion,
287288
originalException);
288289
}
290+
else if (type == "UsageError")
291+
{
292+
wrapperException = new WrapperUsageException(description, EngineName, _engineVersion,
293+
originalException);
294+
wrapperException.Description = description;
295+
296+
return wrapperException;
297+
}
289298
else
290299
{
291300
var errorLocationItems = new ErrorLocationItem[0];

src/JavaScriptEngineSwitcher.Node/Resources/engine-helpers.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1-
const GENERATED_FUNCTION_CALL_FILE_NAME = "JavaScriptEngineSwitcher.Node.Resources.generated-function-call.js";
1+
/*jshint esversion: 6*/
2+
/*globals global, module, require*/
3+
const GENERATED_FUNCTION_CALL_FILE_NAME = "JavaScriptEngineSwitcher.Node.Resources.generated-function-call.js";
24
let vm = require('vm');
35
let contexts = new Map();
46

7+
class UsageError extends Error {
8+
constructor(...args) {
9+
super(...args);
10+
this.name = 'UsageError';
11+
12+
Error.captureStackTrace(this, UsageError);
13+
}
14+
}
15+
16+
function getContext(engineId) {
17+
if (!contexts.has(engineId)) {
18+
throw new UsageError("JavaScriptEngineSwitcher.Node module cannot work correctly " +
19+
"when the Node JS service is configured to work in the multi-process mode of " +
20+
"Jering.Javascript.NodeJS library (https://github.com/JeringTech/Javascript.NodeJS#concurrency), " +
21+
"because in this mode it is not possible to store a state of JS engine."
22+
);
23+
}
24+
25+
return contexts.get(engineId);
26+
}
27+
528
module.exports = {
629
addContext: (callback, engineId, useBuiltinLibrary) => {
730
let sandboxPrototype = useBuiltinLibrary ? global : null;
@@ -19,7 +42,7 @@ module.exports = {
1942
},
2043

2144
evaluate: (callback, engineId, expression, documentName, timeout) => {
22-
let context = contexts.get(engineId);
45+
let context = getContext(engineId);
2346
let options = { filename: documentName };
2447
if (timeout > 0) {
2548
options.timeout = timeout;
@@ -30,7 +53,7 @@ module.exports = {
3053
},
3154

3255
execute: (callback, engineId, code, documentName, timeout) => {
33-
let context = contexts.get(engineId);
56+
let context = getContext(engineId);
3457
let options = { filename: documentName };
3558
if (timeout > 0) {
3659
options.timeout = timeout;
@@ -42,7 +65,7 @@ module.exports = {
4265
},
4366

4467
callFunction: (callback, engineId, functionName, args, timeout) => {
45-
let context = contexts.get(engineId);
68+
let context = getContext(engineId);
4669
let result;
4770

4871
if (timeout <= 0) {
@@ -69,7 +92,7 @@ module.exports = {
6992
expression += JSON.stringify(args[argIndex]);
7093
}
7194

72-
expression += ');'
95+
expression += ');';
7396
}
7497
else {
7598
expression = `${functionName}();`;
@@ -82,28 +105,28 @@ module.exports = {
82105
},
83106

84107
hasVariable: (callback, engineId, variableName) => {
85-
let context = contexts.get(engineId);
108+
let context = getContext(engineId);
86109
let result = typeof context[variableName] !== 'undefined';
87110

88111
callback(null, result);
89112
},
90113

91114
getVariableValue: (callback, engineId, variableName) => {
92-
let context = contexts.get(engineId);
115+
let context = getContext(engineId);
93116
let result = context[variableName];
94117

95118
callback(null, result);
96119
},
97120

98121
setVariableValue: (callback, engineId, variableName, value) => {
99-
let context = contexts.get(engineId);
122+
let context = getContext(engineId);
100123
context[variableName] = value;
101124

102125
callback(null);
103126
},
104127

105128
removeVariable: (callback, engineId, variableName) => {
106-
let context = contexts.get(engineId);
129+
let context = getContext(engineId);
107130
if (typeof context[variableName] !== 'undefined') {
108131
delete context[variableName];
109132
}

src/JavaScriptEngineSwitcher.Node/readme.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
=============
2323
RELEASE NOTES
2424
=============
25-
Jering.Javascript.NodeJS was updated to version 5.4.0.
25+
Added a handling of errors that occur when switching to multi-process mode of
26+
the Jering.Javascript.NodeJS library.
2627

2728
=============
2829
DOCUMENTATION

0 commit comments

Comments
 (0)