์๋ ๋ฐ์ค์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ ๋ vm2๊ฐ "์ฌ๋ฐ๋ฅธ"์คํ ์ถ์ ์ ํ์ ํ ์ ์๋ค๋ฉด ์ข์ ๊ฒ์ ๋๋ค.
์๋ฅผ ๋ค์ด, ํ์ฌ ํ์๋๋ ๋ด์ฉ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
/home/user/box-js/node_modules/vm2/lib/main.js:213
throw this._internal.Decontextify.value(e);
^
Error: foobar
at Object.log (/home/user/box-js/analyze.js:248:10)
at Object.apply (/home/user/box-js/node_modules/vm2/lib/contextify.js:288:34)
at vm.js:491:9
at ContextifyScript.Script.runInContext (vm.js:53:29)
at VM.run (/home/user/box-js/node_modules/vm2/lib/main.js:207:72)
at Object.<anonymous> (/home/user/box-js/analyze.js:383:5)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
์ค๋ฅ๋ฅผ ๋ฐ์์ํจ ํจ์๋ฅผ ํธ์ถ ํ (์๋ ๋ฐ์ค ์ฝ๋์์) ์ด๋ค ์ค์ด ์์ธ์ธ์ง ์์๋ด๋ ๋ฐฉ๋ฒ์ด ์์ต๋๊น?
์ด๊ฒ์ ๊ต์ฅ ํ ๊ฒ์ ๋๋ค. ๋๋ ์ง๊ธ ์ฝ๋ฉ ์ค์๋ฅผ ์ถ์ ํ๋ ค๊ณ ๋ ธ๋ ฅํ๊ณ ์์ผ๋ฉฐ ์ค ๋ฒํธ ์์ด๋ ์์ฒญ๋๊ฒ ํ๋ ์๊ฐ์ ๋ณด๋ด๊ณ ์์ต๋๋ค.
์์ ํ๋ค๊ณ ์๊ณ ์๋ ํ ์คํธ ์ผ์ด์ค๋ฅผ ์คํ์ค์ธ ๊ฒฝ์ฐ ์ผ์์ ์ผ๋ก vm2๋ฅผ ๊ธฐ๋ณธ vm์ผ๋ก ๋์ฒดํ์ฌ ์ค๋ฅ๋ฅผ ์ถ์ ํ ์ ์์ต๋๋ค.
์ ๋์ด ๊ธฐ๋ฅ์ ์ํฉ๋๋ค. ๋๊ตฐ๊ฐ๊ฐ ๋๋ฅผ ์ฌ๋ฐ๋ฅธ ๋ฐฉํฅ์ผ๋ก ์๋ดํ๋ฉด ๊ธ์ ์ธ ์์์ด์ ๊ธฐ์ฉ๋๋ค.
์ด ๋ฌธ์ ๋ ์ต๊ทผ ํ๋์ด ์์๊ธฐ ๋๋ฌธ์ ์๋์ผ๋ก ์ค๋๋ ๊ฒ์ผ๋ก ํ์๋์์ต๋๋ค. ๋ ์ด์ ํ๋์ด ๋ฐ์ํ์ง ์์ผ๋ฉด ํ์๋ฉ๋๋ค. ๊ทํ์ ๊ธฐ์ฌ์ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
API ๋น ํธํ์ฑ์ด ์๊ธฐ ๋๋ฌธ์ vm2๋ฅผ Native vm์ผ๋ก ๋์ฒดํ๋ ๊ฒ์ ์ฌ์ด ์ผ์ด ์๋๋๋ค.
@patriksimek , ์ ๋ ์์ธ ํ์ / ์ฌ ์์ฑ๊ธฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ๋จํ ๊ตฌํ์ ๊ณ ์ํ์ต๋๋ค. ์ ํ์ ์ค๋ฅ ์ฒ๋ฆฌ๊ธฐ๋ก vm2์ ํตํฉํ๋ ๊ฒ์ด ๊ฐ์น๊ฐ ์๋ค๊ณ ์๊ฐํฉ๋๋ค.
const StackTracey = require ('stacktracey');
const { VM } = require('vm2');
const sourceCode = `function f() {
throw Exception('e');
}
f();`;
const scriptName = "hello_world.js";
process.on("uncaughtException", e => {
if (!e.stack.includes("/node_modules/vm2/")) {
// This is not a vm2 error, so print it normally
console.log(e);
return;
}
const oldStack = new StackTracey(e);
const newStack = [];
for (const line of oldStack) {
// Discard internal code
if (line.file.includes("/cjs"))
continue;
if (line.thirdParty && line.file.includes("/node_modules/vm2/"))
continue;
if (line.callee == "Script.runInContext")
continue;
// Replace the default filename with the user-provided one
if (line.fileName == "vm.js")
line.fileRelative = line.fileShort = line.fileName = scriptName;
newStack.push(line);
}
console.log("[vm2] A clean stack trace follows:");
console.log(new StackTracey(newStack).pretty);
});
const vm = new VM();
vm.run(sourceCode, scriptName);
์์ ์ฝ๋๋ ๋ค์์ ์ธ์ํฉ๋๋ค.
ReferenceError: Exception is not defined
at f (vm.js:2:2)
at vm.js:4:1
at Script.runInContext (vm.js:135:20)
at VM.run (/tmp/vm2-test/node_modules/vm2/lib/main.js:210:72)
at Object.<anonymous> (/tmp/vm2-test/index.js:33:4)
at Module._compile (internal/modules/cjs/loader.js:805:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:816:10)
at Module.load (internal/modules/cjs/loader.js:672:32)
at tryModuleLoad (internal/modules/cjs/loader.js:612:12)
at Function.Module._load (internal/modules/cjs/loader.js:604:3)
[vm2] A clean stack trace follows:
at f hello_world.js:2
at hello_world.js:4
at <anonymous> index.js:33 vm.run(sourceCode, scriptName);
์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํ ์คํธ ํ ๊ธฐํ๊ฐ ์์์ง๋ง ์ต๊ทผ์ vm ๋ชจ๋์ ์ฌ์ฉํ๊ณ ์ vm.Script๋ฅผ ๋ง๋ค ๋ ์ ์ ํ ๋ผ์ธ ์คํ์ ์ ์ค์ ํ์ฌ ์ ํํ ์คํ ์ถ์ ๋ฒํธ๋ฅผ ๋ฌ์ฑํ์ต๋๋ค.
์ด ๋ฌธ์ ๋ ์ต๊ทผ ํ๋์ด ์์๊ธฐ ๋๋ฌธ์ ์๋์ผ๋ก ์ค๋๋ ๊ฒ์ผ๋ก ํ์๋์์ต๋๋ค. ๋ ์ด์ ํ๋์ด ๋ฐ์ํ์ง ์์ผ๋ฉด ํ์๋ฉ๋๋ค. ๊ทํ์ ๊ธฐ์ฌ์ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
์ด ๋ฌธ์ + ์ ์์ด ์ฌ์ ํ ์ ์ ๊ด๋ จ์ด ์์ผ๋ฏ๋ก ์๊ฒฌ์ ๋งํ๊ณ ์ถ์ต๋๋ค!
๋์๊ฒ๋ ๋งค์ฐ ๊ด๋ จ์ด ์์ต๋๋ค.
@patriksimek ์ @CapacitorSet ์ ๊ตฌํ์ด๋ ๊ทธ ๋ผ์ธ์ ๋ฐ๋ฅธ ๋ฌด์ธ๊ฐ๋ฅผ ํ๋ก์ ํธ์ ํตํฉ ํ ์ ์์ต๋๊น?
๊ฐ์ฌ.
Patrik, ๊ตฌํ ์ค์ผ์น๊ฐ ์ ๋ฐ์ ์ผ๋ก ๊ด์ฐฎ์ ๋ณด์ ๋๊น? ๊ทธ๋ ๋ค๋ฉด PR์ ํ ์ ์๊ณ ๊ฑฐ๊ธฐ์ ์ผํ ์ ์์ต๋๋ค.
@CapacitorSet ์, PR์ ๊ฒํ ํด ๋๋ฆฌ๊ฒ ์ต๋๋ค. ๋ช ๊ฐ์ง ์ฐธ๊ณ ์ฌํญ :
vm.run
๋ฉ์๋๋ ๋ ๋ฒ์งธ ๋งค๊ฐ ๋ณ์๋ฅผ ํ์ฉํ์ง ์์ต๋๋ค. ์ด๊ฒ์ด ์คํฌ๋ฆฝํธ ์ด๋ฆ์ด ๋ฌด์๋๋ ์ด์ ์
๋๋ค. ํ์ฌ๋ ๋จผ์ ์ง์ ๋ ์ด๋ฆ์ผ๋ก VMScript๋ฅผ ๋ง๋ค์ด์ผํฉ๋๋ค. ๊ทธ๋ฌ๋ ๊ทธ๊ฒ์ ์ฝ๊ฒ ๊ฐ์ ๋ ์ ์์ต๋๋ค.์ด ๋ฌธ์ ๋ฅผ ๊ตฌ๋ ํ๋ ์ฌ๋๋ค์๊ฒ ์๋ฆฌ๊ธฐ ์ํด ๋๊ธ์ ๋จ๊ธฐ๋ ๊ฒ๋ง์ผ๋ก๋ ์งํ ์ํฉ์ ์ถ์ ํ ์ ์๋๋ก PR์ ์ด์์ต๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ด ๋ฌธ์ ๋ฅผ ๊ตฌ๋ ํ๋ ์ฌ๋๋ค์๊ฒ ์๋ฆฌ๊ธฐ ์ํด ๋๊ธ์ ๋จ๊ธฐ๋ ๊ฒ๋ง์ผ๋ก๋ ์งํ ์ํฉ์ ์ถ์ ํ ์ ์๋๋ก PR์ ์ด์์ต๋๋ค.