-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#61] 클라이언트에 js 엔진 올리기 #83
The head ref may contain hidden characters: "61-\uD074\uB77C\uC774\uC5B8\uD2B8\uC5D0-js-\uC5D4\uC9C4-\uC62C\uB9AC\uAE30"
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { QuickJSContext, QuickJSWASMModule } from 'quickjs-emscripten'; | ||
import { getQuickJS } from 'quickjs-emscripten'; | ||
|
||
const MEM_LIMIT = 1024 * 1024; // 1GB | ||
const STACK_LIMIT = 1024 * 512; // 500MB | ||
|
||
export async function evaluate(code: string, params: string) { | ||
const QuickJS = await getQuickJS(); | ||
const runtime = createRuntime(QuickJS); | ||
const vm = runtime.newContext(); | ||
|
||
try { | ||
return evalCode(vm, code, params); | ||
} finally { | ||
vm.dispose(); | ||
runtime.dispose(); | ||
} | ||
} | ||
Comment on lines
+12
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. evalCode가 정상작동하면 return하고 아니면 finally가 돌아가는 거 같은데, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 와우 😎 |
||
|
||
const createRuntime = (quickjs: QuickJSWASMModule) => { | ||
const runtime = quickjs.newRuntime(); | ||
runtime.setMemoryLimit(MEM_LIMIT); | ||
runtime.setMaxStackSize(STACK_LIMIT); | ||
|
||
return runtime; | ||
}; | ||
|
||
const evalCode = (vm: QuickJSContext, code: string, params: string) => { | ||
const script = toRunableScript(code, params); | ||
|
||
const startTime = performance.now(); | ||
Comment on lines
+28
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 별 게 아니긴 한데 궁금해서요! 29번째 줄과 31번째 줄 사이에 공백을 넣으신 이유가 있나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 가독성을 위해 논리가 바뀌는 영역을 분리한거에요.
순서로 분리된 겁니다. |
||
const result = vm.unwrapResult(vm.evalCode(script)); | ||
const endTime = performance.now(); | ||
const value = vm.dump(result); | ||
result.dispose(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 정상적으로 돌아가면 여기까지 오게 되고 여기서 해제해주는 건가요?! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네 그렇게 생각했는데 다시 보니까 엣지케이스가 좀 있네요 function solution() {
throw Error("err");
} 위와 같은 코드가 들어오면 dispose를 거치지 않게 되고 있습니다. 우선은 다른 메모리 할당해제가 잘 일어나서 누수가 없지 않을까? 생각되긴 하는데, 이건 그냥 추측이구요. 이런 케이스를 보완할 수 있는 방법을 좀 찾아보고 추가해볼게요 |
||
|
||
return { | ||
time: endTime - startTime, | ||
result: value, | ||
}; | ||
}; | ||
|
||
const toRunableScript = (code: string, params: string) => { | ||
return ` | ||
${code}\n | ||
solution(${params}); | ||
`; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻 완벽한 모듈화네요