Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions crates/deno-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ fn op_custom_print(#[string] message: String, is_err: bool) -> Result<(), AnyErr
Ok(())
}

/// 커스텀 확장 정의
extension!(
executejs_runtime,
ops = [op_console_log, op_alert, op_custom_print],
Expand Down Expand Up @@ -146,16 +145,33 @@ impl DenoExecutor {
return Err(anyhow::anyhow!("Bootstrap 실행 실패: {}", e));
}

// 사용자 코드를 래핑하여 마지막 표현식의 결과를 자동으로 출력
// eval을 사용하여 마지막 표현식의 결과를 캡처하고 출력
let wrapped_code = format!(
r#"
(function() {{
try {{
const code_output = eval(`{}`);
if (code_output !== undefined) {{
Deno.core.ops.op_console_log(String(code_output));
}}
return code_output;
}} catch (e) {{
// eval 실패 시 원본 코드를 그대로 실행
throw e;
}}
}})();
"#,
code.replace('`', r"\`").replace('\\', r"\\")
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The escaping order is incorrect and will cause double-escaping issues. Backslashes must be escaped before backticks to prevent already-escaped sequences from being corrupted. For example, the input \\ would become \\\\ (correct), but then backticks in that result would be incorrectly escaped. Change to code.replace('\\', r\"\\\\").replace('', r"`")` to escape backslashes first.

Suggested change
code.replace('`', r"\`").replace('\\', r"\\")
code.replace('\\', r"\\").replace('`', r"\`")

Copilot uses AI. Check for mistakes.
Comment on lines +154 to +165
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment states 'eval 실패 시 원본 코드를 그대로 실행' (when eval fails, execute original code as-is), but the code throws the error instead of falling back to executing the original code. Either update the comment to reflect the actual behavior ('eval 실패 시 에러를 전파' - propagate error when eval fails) or implement the fallback logic described in the comment.

Suggested change
const code_output = eval(`{}`);
if (code_output !== undefined) {{
Deno.core.ops.op_console_log(String(code_output));
}}
return code_output;
}} catch (e) {{
// eval 실패 시 원본 코드를 그대로 실행
throw e;
}}
}})();
"#,
code.replace('`', r"\`").replace('\\', r"\\")
const code_output = eval(`{code}`);
if (code_output !== undefined) {{
Deno.core.ops.op_console_log(String(code_output));
}}
return code_output;
}} catch (e) {{
// eval 실패 시 원본 코드를 그대로 실행
try {
const fallback_output = (function() {{ {} }})();
if (fallback_output !== undefined) {{
Deno.core.ops.op_console_log(String(fallback_output));
}}
return fallback_output;
} catch (fallbackError) {{
throw fallbackError;
}}
const fallback_output = (function() {{ {code} }})();
if (fallback_output !== undefined) {{
Deno.core.ops.op_console_log(String(fallback_output));
}}
return fallback_output;
}} catch (fallbackError) {{
throw fallbackError;
}}
}}
}})();
"#,
code = code.replace('`', r"\`").replace('\\', r"\\")

Copilot uses AI. Check for mistakes.
);

// 코드 실행
let result = js_runtime.execute_script("[executejs:user_code]", code)?;
let result = js_runtime.execute_script("[executejs:user_code]", wrapped_code)?;

// 이벤트 루프 실행 (Promise 처리) - 블로킹 방식으로 변경
let rt = tokio::runtime::Handle::current();
rt.block_on(async { js_runtime.run_event_loop(Default::default()).await })?;

// 결과 처리
let _ = result;

// 출력 버퍼에서 결과 가져오기
let output = output_buffer.lock().unwrap();
let result_text = output.get_output();
Expand Down