mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 12:19:12 +00:00
refactor(core): convert core.print() to a builtin op (#10436)
This commit is contained in:
parent
c9ac851b90
commit
ea917384fe
6 changed files with 32 additions and 46 deletions
|
@ -34,15 +34,16 @@ delete Object.prototype.__proto__;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function printStderr(msg) {
|
||||||
|
core.print(msg, true);
|
||||||
|
}
|
||||||
|
|
||||||
function debug(...args) {
|
function debug(...args) {
|
||||||
if (logDebug) {
|
if (logDebug) {
|
||||||
const stringifiedArgs = args.map((arg) =>
|
const stringifiedArgs = args.map((arg) =>
|
||||||
typeof arg === "string" ? arg : JSON.stringify(arg)
|
typeof arg === "string" ? arg : JSON.stringify(arg)
|
||||||
).join(" ");
|
).join(" ");
|
||||||
// adding a non-zero integer value to the end of the debug string causes
|
printStderr(`DEBUG ${logSource} - ${stringifiedArgs}\n`);
|
||||||
// the message to be printed to stderr instead of stdout, which is better
|
|
||||||
// aligned to the behaviour of debug messages
|
|
||||||
core.print(`DEBUG ${logSource} - ${stringifiedArgs}\n`, 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +53,7 @@ delete Object.prototype.__proto__;
|
||||||
? String(arg)
|
? String(arg)
|
||||||
: JSON.stringify(arg)
|
: JSON.stringify(arg)
|
||||||
).join(" ");
|
).join(" ");
|
||||||
core.print(`ERROR ${logSource} = ${stringifiedArgs}\n`, 1);
|
printStderr(`ERROR ${logSource} = ${stringifiedArgs}\n`);
|
||||||
}
|
}
|
||||||
|
|
||||||
class AssertionError extends Error {
|
class AssertionError extends Error {
|
||||||
|
|
2
cli/tsc/compiler.d.ts
vendored
2
cli/tsc/compiler.d.ts
vendored
|
@ -36,7 +36,7 @@ declare global {
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
opSync<T>(name: string, params: T): any;
|
opSync<T>(name: string, params: T): any;
|
||||||
ops(): void;
|
ops(): void;
|
||||||
print(msg: string, code?: number): void;
|
print(msg: string, stderr: bool): void;
|
||||||
registerErrorClass(
|
registerErrorClass(
|
||||||
name: string,
|
name: string,
|
||||||
Ctor: typeof Error,
|
Ctor: typeof Error,
|
||||||
|
|
|
@ -13,7 +13,6 @@ use serde::Serialize;
|
||||||
use serde_v8::to_v8;
|
use serde_v8::to_v8;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use std::io::{stdout, Write};
|
|
||||||
use std::option::Option;
|
use std::option::Option;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
use v8::MapFnTo;
|
use v8::MapFnTo;
|
||||||
|
@ -21,9 +20,6 @@ use v8::MapFnTo;
|
||||||
lazy_static::lazy_static! {
|
lazy_static::lazy_static! {
|
||||||
pub static ref EXTERNAL_REFERENCES: v8::ExternalReferences =
|
pub static ref EXTERNAL_REFERENCES: v8::ExternalReferences =
|
||||||
v8::ExternalReferences::new(&[
|
v8::ExternalReferences::new(&[
|
||||||
v8::ExternalReference {
|
|
||||||
function: print.map_fn_to()
|
|
||||||
},
|
|
||||||
v8::ExternalReference {
|
v8::ExternalReference {
|
||||||
function: opcall.map_fn_to()
|
function: opcall.map_fn_to()
|
||||||
},
|
},
|
||||||
|
@ -117,7 +113,6 @@ pub fn initialize_context<'s>(
|
||||||
deno_val.set(scope, core_key.into(), core_val.into());
|
deno_val.set(scope, core_key.into(), core_val.into());
|
||||||
|
|
||||||
// Bind functions to Deno.core.*
|
// Bind functions to Deno.core.*
|
||||||
set_func(scope, core_val, "print", print);
|
|
||||||
set_func(scope, core_val, "opcall", opcall);
|
set_func(scope, core_val, "opcall", opcall);
|
||||||
set_func(
|
set_func(
|
||||||
scope,
|
scope,
|
||||||
|
@ -268,41 +263,6 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print(
|
|
||||||
scope: &mut v8::HandleScope,
|
|
||||||
args: v8::FunctionCallbackArguments,
|
|
||||||
_rv: v8::ReturnValue,
|
|
||||||
) {
|
|
||||||
let arg_len = args.length();
|
|
||||||
if !(0..=2).contains(&arg_len) {
|
|
||||||
return throw_type_error(scope, "Expected a maximum of 2 arguments.");
|
|
||||||
}
|
|
||||||
|
|
||||||
let obj = args.get(0);
|
|
||||||
let is_err_arg = args.get(1);
|
|
||||||
|
|
||||||
let mut is_err = false;
|
|
||||||
if arg_len == 2 {
|
|
||||||
let int_val = match is_err_arg.integer_value(scope) {
|
|
||||||
Some(v) => v,
|
|
||||||
None => return throw_type_error(scope, "Invalid argument. Argument 2 should indicate whether or not to print to stderr."),
|
|
||||||
};
|
|
||||||
is_err = int_val != 0;
|
|
||||||
};
|
|
||||||
let tc_scope = &mut v8::TryCatch::new(scope);
|
|
||||||
let str_ = match obj.to_string(tc_scope) {
|
|
||||||
Some(s) => s,
|
|
||||||
None => v8::String::new(tc_scope, "").unwrap(),
|
|
||||||
};
|
|
||||||
if is_err {
|
|
||||||
eprint!("{}", str_.to_rust_string_lossy(tc_scope));
|
|
||||||
stdout().flush().unwrap();
|
|
||||||
} else {
|
|
||||||
print!("{}", str_.to_rust_string_lossy(tc_scope));
|
|
||||||
stdout().flush().unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn opcall<'s>(
|
fn opcall<'s>(
|
||||||
scope: &mut v8::HandleScope<'s>,
|
scope: &mut v8::HandleScope<'s>,
|
||||||
args: v8::FunctionCallbackArguments,
|
args: v8::FunctionCallbackArguments,
|
||||||
|
|
|
@ -124,6 +124,10 @@
|
||||||
opSync("op_close", rid);
|
opSync("op_close", rid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function print(str, isErr = false) {
|
||||||
|
opSync("op_print", [str, isErr]);
|
||||||
|
}
|
||||||
|
|
||||||
// Provide bootstrap namespace
|
// Provide bootstrap namespace
|
||||||
window.__bootstrap = {};
|
window.__bootstrap = {};
|
||||||
// Extra Deno.core.* exports
|
// Extra Deno.core.* exports
|
||||||
|
@ -132,6 +136,7 @@
|
||||||
opSync,
|
opSync,
|
||||||
ops,
|
ops,
|
||||||
close,
|
close,
|
||||||
|
print,
|
||||||
resources,
|
resources,
|
||||||
registerErrorClass,
|
registerErrorClass,
|
||||||
handleAsyncMsgFromRust,
|
handleAsyncMsgFromRust,
|
||||||
|
|
|
@ -65,6 +65,7 @@ pub use crate::ops::OpState;
|
||||||
pub use crate::ops::OpTable;
|
pub use crate::ops::OpTable;
|
||||||
pub use crate::ops::PromiseId;
|
pub use crate::ops::PromiseId;
|
||||||
pub use crate::ops_builtin::op_close;
|
pub use crate::ops_builtin::op_close;
|
||||||
|
pub use crate::ops_builtin::op_print;
|
||||||
pub use crate::ops_builtin::op_resources;
|
pub use crate::ops_builtin::op_resources;
|
||||||
pub use crate::ops_json::op_async;
|
pub use crate::ops_json::op_async;
|
||||||
pub use crate::ops_json::op_sync;
|
pub use crate::ops_json::op_sync;
|
||||||
|
|
|
@ -7,6 +7,7 @@ use crate::resources::ResourceId;
|
||||||
use crate::Extension;
|
use crate::Extension;
|
||||||
use crate::OpState;
|
use crate::OpState;
|
||||||
use crate::ZeroCopyBuf;
|
use crate::ZeroCopyBuf;
|
||||||
|
use std::io::{stdout, Write};
|
||||||
|
|
||||||
pub(crate) fn init_builtins() -> Extension {
|
pub(crate) fn init_builtins() -> Extension {
|
||||||
Extension::builder()
|
Extension::builder()
|
||||||
|
@ -17,6 +18,7 @@ pub(crate) fn init_builtins() -> Extension {
|
||||||
))
|
))
|
||||||
.ops(vec![
|
.ops(vec![
|
||||||
("op_close", op_sync(op_close)),
|
("op_close", op_sync(op_close)),
|
||||||
|
("op_print", op_sync(op_print)),
|
||||||
("op_resources", op_sync(op_resources)),
|
("op_resources", op_sync(op_resources)),
|
||||||
])
|
])
|
||||||
.build()
|
.build()
|
||||||
|
@ -52,3 +54,20 @@ pub fn op_close(
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Builtin utility to print to stdout/stderr
|
||||||
|
pub fn op_print(
|
||||||
|
_state: &mut OpState,
|
||||||
|
args: (String, bool),
|
||||||
|
_zero_copy: Option<ZeroCopyBuf>,
|
||||||
|
) -> Result<(), AnyError> {
|
||||||
|
let (msg, is_err) = args;
|
||||||
|
if is_err {
|
||||||
|
eprint!("{}", msg);
|
||||||
|
stdout().flush().unwrap();
|
||||||
|
} else {
|
||||||
|
print!("{}", msg);
|
||||||
|
stdout().flush().unwrap();
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue