1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-01 16:51:13 -05:00

Merge branch 'main' into support_create_connection

Signed-off-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
Yoshiya Hinosawa 2024-10-17 22:18:36 +09:00 committed by GitHub
commit d6af2c918c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 301 additions and 437 deletions

View file

@ -264,6 +264,16 @@ fn napi_define_class<'s>(
Err(status) => return status, Err(status) => return status,
}; };
let mut accessor_property = v8::PropertyAttribute::NONE;
if p.attributes & napi_enumerable == 0 {
accessor_property = accessor_property | v8::PropertyAttribute::DONT_ENUM;
}
if p.attributes & napi_configurable == 0 {
accessor_property =
accessor_property | v8::PropertyAttribute::DONT_DELETE;
}
if p.getter.is_some() || p.setter.is_some() { if p.getter.is_some() || p.setter.is_some() {
let getter = p.getter.map(|g| { let getter = p.getter.map(|g| {
create_function_template(&mut env.scope(), env_ptr, None, g, p.data) create_function_template(&mut env.scope(), env_ptr, None, g, p.data)
@ -271,8 +281,6 @@ fn napi_define_class<'s>(
let setter = p.setter.map(|s| { let setter = p.setter.map(|s| {
create_function_template(&mut env.scope(), env_ptr, None, s, p.data) create_function_template(&mut env.scope(), env_ptr, None, s, p.data)
}); });
let mut accessor_property = v8::PropertyAttribute::NONE;
if getter.is_some() if getter.is_some()
&& setter.is_some() && setter.is_some()
&& (p.attributes & napi_writable) == 0 && (p.attributes & napi_writable) == 0
@ -280,15 +288,6 @@ fn napi_define_class<'s>(
accessor_property = accessor_property =
accessor_property | v8::PropertyAttribute::READ_ONLY; accessor_property | v8::PropertyAttribute::READ_ONLY;
} }
if p.attributes & napi_enumerable == 0 {
accessor_property =
accessor_property | v8::PropertyAttribute::DONT_ENUM;
}
if p.attributes & napi_configurable == 0 {
accessor_property =
accessor_property | v8::PropertyAttribute::DONT_DELETE;
}
let proto = tpl.prototype_template(&mut env.scope()); let proto = tpl.prototype_template(&mut env.scope());
proto.set_accessor_property(name, getter, setter, accessor_property); proto.set_accessor_property(name, getter, setter, accessor_property);
} else if let Some(method) = p.method { } else if let Some(method) = p.method {
@ -300,10 +299,14 @@ fn napi_define_class<'s>(
p.data, p.data,
); );
let proto = tpl.prototype_template(&mut env.scope()); let proto = tpl.prototype_template(&mut env.scope());
proto.set(name, function.into()); proto.set_with_attr(name, function.into(), accessor_property);
} else { } else {
let proto = tpl.prototype_template(&mut env.scope()); let proto = tpl.prototype_template(&mut env.scope());
proto.set(name, p.value.unwrap().into()); if (p.attributes & napi_writable) == 0 {
accessor_property =
accessor_property | v8::PropertyAttribute::READ_ONLY;
}
proto.set_with_attr(name, p.value.unwrap().into(), accessor_property);
} }
} }

View file

@ -76,7 +76,11 @@ import {
ReadableStreamPrototype, ReadableStreamPrototype,
resourceForReadableStream, resourceForReadableStream,
} from "ext:deno_web/06_streams.js"; } from "ext:deno_web/06_streams.js";
import { listen, listenOptionApiName, TcpConn } from "ext:deno_net/01_net.js"; import {
listen,
listenOptionApiName,
UpgradedConn,
} from "ext:deno_net/01_net.js";
import { hasTlsKeyPairOptions, listenTls } from "ext:deno_net/02_tls.js"; import { hasTlsKeyPairOptions, listenTls } from "ext:deno_net/02_tls.js";
import { SymbolAsyncDispose } from "ext:deno_web/00_infra.js"; import { SymbolAsyncDispose } from "ext:deno_web/00_infra.js";
@ -189,7 +193,7 @@ class InnerRequest {
const upgradeRid = op_http_upgrade_raw(external); const upgradeRid = op_http_upgrade_raw(external);
const conn = new TcpConn( const conn = new UpgradedConn(
upgradeRid, upgradeRid,
underlyingConn?.remoteAddr, underlyingConn?.remoteAddr,
underlyingConn?.localAddr, underlyingConn?.localAddr,

View file

@ -194,6 +194,20 @@ class Conn {
} }
} }
class UpgradedConn extends Conn {
#rid = 0;
constructor(rid, remoteAddr, localAddr) {
super(rid, remoteAddr, localAddr);
ObjectDefineProperty(this, internalRidSymbol, {
__proto__: null,
enumerable: false,
value: rid,
});
this.#rid = rid;
}
}
class TcpConn extends Conn { class TcpConn extends Conn {
#rid = 0; #rid = 0;
@ -601,5 +615,6 @@ export {
resolveDns, resolveDns,
TcpConn, TcpConn,
UnixConn, UnixConn,
UpgradedConn,
validatePort, validatePort,
}; };

View file

@ -66,7 +66,7 @@ import { getTimerDuration } from "ext:deno_node/internal/timers.mjs";
import { serve, upgradeHttpRaw } from "ext:deno_http/00_serve.ts"; import { serve, upgradeHttpRaw } from "ext:deno_http/00_serve.ts";
import { headersEntries } from "ext:deno_fetch/20_headers.js"; import { headersEntries } from "ext:deno_fetch/20_headers.js";
import { resourceForReadableStream } from "ext:deno_web/06_streams.js"; import { resourceForReadableStream } from "ext:deno_web/06_streams.js";
import { TcpConn } from "ext:deno_net/01_net.js"; import { UpgradedConn } from "ext:deno_net/01_net.js";
import { STATUS_CODES } from "node:_http_server"; import { STATUS_CODES } from "node:_http_server";
import { methods as METHODS } from "node:_http_common"; import { methods as METHODS } from "node:_http_common";
@ -513,7 +513,7 @@ class ClientRequest extends OutgoingMessage {
const upgradeRid = await op_node_http_fetch_response_upgrade( const upgradeRid = await op_node_http_fetch_response_upgrade(
res.responseRid, res.responseRid,
); );
const conn = new TcpConn( const conn = new UpgradedConn(
upgradeRid, upgradeRid,
{ {
transport: "tcp", transport: "tcp",

View file

@ -304,7 +304,9 @@ export class TCP extends ConnectionWrap {
* @return An error status code. * @return An error status code.
*/ */
setNoDelay(noDelay: boolean): number { setNoDelay(noDelay: boolean): number {
this[kStreamBaseField].setNoDelay(noDelay); if ("setNoDelay" in this[kStreamBaseField]) {
this[kStreamBaseField].setNoDelay(noDelay);
}
return 0; return 0;
} }

View file

@ -147,438 +147,226 @@ impl Resource for SignalStreamResource {
} }
} }
#[cfg(target_os = "freebsd")] macro_rules! first_literal {
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> { ($head:literal $(, $tail:literal)*) => {
match s { $head
"SIGHUP" => Ok(1), };
"SIGINT" => Ok(2), }
"SIGQUIT" => Ok(3), macro_rules! signal_dict {
"SIGILL" => Ok(4), ($error_msg:expr, $(($number:literal, $($name:literal)|+)),*) => {
"SIGTRAP" => Ok(5), pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> {
"SIGIOT" => Ok(6), match s {
"SIGABRT" => Ok(6), $($($name)|* => Ok($number),)*
"SIGEMT" => Ok(7), _ => Err(type_error($error_msg(s))),
"SIGFPE" => Ok(8), }
"SIGKILL" => Ok(9), }
"SIGBUS" => Ok(10),
"SIGSEGV" => Ok(11), pub fn signal_int_to_str(s: libc::c_int) -> Result<&'static str, AnyError> {
"SIGSYS" => Ok(12), match s {
"SIGPIPE" => Ok(13), $($number => Ok(first_literal!($($name),+)),)*
"SIGALRM" => Ok(14), _ => Err(type_error($error_msg(s))),
"SIGTERM" => Ok(15), }
"SIGURG" => Ok(16), }
"SIGSTOP" => Ok(17),
"SIGTSTP" => Ok(18),
"SIGCONT" => Ok(19),
"SIGCHLD" => Ok(20),
"SIGTTIN" => Ok(21),
"SIGTTOU" => Ok(22),
"SIGIO" => Ok(23),
"SIGXCPU" => Ok(24),
"SIGXFSZ" => Ok(25),
"SIGVTALRM" => Ok(26),
"SIGPROF" => Ok(27),
"SIGWINCH" => Ok(28),
"SIGINFO" => Ok(29),
"SIGUSR1" => Ok(30),
"SIGUSR2" => Ok(31),
"SIGTHR" => Ok(32),
"SIGLIBRT" => Ok(33),
_ => Err(type_error(format!("Invalid signal : {}", s))),
} }
} }
#[cfg(target_os = "freebsd")] #[cfg(target_os = "freebsd")]
pub fn signal_int_to_str(s: libc::c_int) -> Result<&'static str, AnyError> { signal_dict!(
match s { |s| { format!("Invalid signal : {}", s) },
1 => Ok("SIGHUP"), (1, "SIGHUP"),
2 => Ok("SIGINT"), (2, "SIGINT"),
3 => Ok("SIGQUIT"), (3, "SIGQUIT"),
4 => Ok("SIGILL"), (4, "SIGILL"),
5 => Ok("SIGTRAP"), (5, "SIGTRAP"),
6 => Ok("SIGABRT"), (6, "SIGABRT" | "SIGIOT"),
7 => Ok("SIGEMT"), (7, "SIGEMT"),
8 => Ok("SIGFPE"), (8, "SIGFPE"),
9 => Ok("SIGKILL"), (9, "SIGKILL"),
10 => Ok("SIGBUS"), (10, "SIGBUS"),
11 => Ok("SIGSEGV"), (11, "SIGSEGV"),
12 => Ok("SIGSYS"), (12, "SIGSYS"),
13 => Ok("SIGPIPE"), (13, "SIGPIPE"),
14 => Ok("SIGALRM"), (14, "SIGALRM"),
15 => Ok("SIGTERM"), (15, "SIGTERM"),
16 => Ok("SIGURG"), (16, "SIGURG"),
17 => Ok("SIGSTOP"), (17, "SIGSTOP"),
18 => Ok("SIGTSTP"), (18, "SIGTSTP"),
19 => Ok("SIGCONT"), (19, "SIGCONT"),
20 => Ok("SIGCHLD"), (20, "SIGCHLD"),
21 => Ok("SIGTTIN"), (21, "SIGTTIN"),
22 => Ok("SIGTTOU"), (22, "SIGTTOU"),
23 => Ok("SIGIO"), (23, "SIGIO"),
24 => Ok("SIGXCPU"), (24, "SIGXCPU"),
25 => Ok("SIGXFSZ"), (25, "SIGXFSZ"),
26 => Ok("SIGVTALRM"), (26, "SIGVTALRM"),
27 => Ok("SIGPROF"), (27, "SIGPROF"),
28 => Ok("SIGWINCH"), (28, "SIGWINCH"),
29 => Ok("SIGINFO"), (29, "SIGINFO"),
30 => Ok("SIGUSR1"), (30, "SIGUSR1"),
31 => Ok("SIGUSR2"), (31, "SIGUSR2"),
32 => Ok("SIGTHR"), (32, "SIGTHR"),
33 => Ok("SIGLIBRT"), (33, "SIGLIBRT")
_ => Err(type_error(format!("Invalid signal : {}", s))), );
}
}
#[cfg(target_os = "openbsd")] #[cfg(target_os = "openbsd")]
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> { signal_dict!(
match s { |s| { format!("Invalid signal : {}", s) },
"SIGHUP" => Ok(1), (1, "SIGHUP"),
"SIGINT" => Ok(2), (2, "SIGINT"),
"SIGQUIT" => Ok(3), (3, "SIGQUIT"),
"SIGILL" => Ok(4), (4, "SIGILL"),
"SIGTRAP" => Ok(5), (5, "SIGTRAP"),
"SIGIOT" => Ok(6), (6, "SIGABRT" | "SIGIOT"),
"SIGABRT" => Ok(6), (7, "SIGEMT"),
"SIGEMT" => Ok(7), (8, "SIGKILL"),
"SIGFPE" => Ok(8), (10, "SIGBUS"),
"SIGKILL" => Ok(9), (11, "SIGSEGV"),
"SIGBUS" => Ok(10), (12, "SIGSYS"),
"SIGSEGV" => Ok(11), (13, "SIGPIPE"),
"SIGSYS" => Ok(12), (14, "SIGALRM"),
"SIGPIPE" => Ok(13), (15, "SIGTERM"),
"SIGALRM" => Ok(14), (16, "SIGURG"),
"SIGTERM" => Ok(15), (17, "SIGSTOP"),
"SIGURG" => Ok(16), (18, "SIGTSTP"),
"SIGSTOP" => Ok(17), (19, "SIGCONT"),
"SIGTSTP" => Ok(18), (20, "SIGCHLD"),
"SIGCONT" => Ok(19), (21, "SIGTTIN"),
"SIGCHLD" => Ok(20), (22, "SIGTTOU"),
"SIGTTIN" => Ok(21), (23, "SIGIO"),
"SIGTTOU" => Ok(22), (24, "SIGXCPU"),
"SIGIO" => Ok(23), (25, "SIGXFSZ"),
"SIGXCPU" => Ok(24), (26, "SIGVTALRM"),
"SIGXFSZ" => Ok(25), (27, "SIGPROF"),
"SIGVTALRM" => Ok(26), (28, "SIGWINCH"),
"SIGPROF" => Ok(27), (29, "SIGINFO"),
"SIGWINCH" => Ok(28), (30, "SIGUSR1"),
"SIGINFO" => Ok(29), (31, "SIGUSR2"),
"SIGUSR1" => Ok(30), (32, "SIGTHR")
"SIGUSR2" => Ok(31), );
"SIGTHR" => Ok(32),
_ => Err(type_error(format!("Invalid signal : {}", s))),
}
}
#[cfg(target_os = "openbsd")]
pub fn signal_int_to_str(s: libc::c_int) -> Result<&'static str, AnyError> {
match s {
1 => Ok("SIGHUP"),
2 => Ok("SIGINT"),
3 => Ok("SIGQUIT"),
4 => Ok("SIGILL"),
5 => Ok("SIGTRAP"),
6 => Ok("SIGABRT"),
7 => Ok("SIGEMT"),
8 => Ok("SIGFPE"),
9 => Ok("SIGKILL"),
10 => Ok("SIGBUS"),
11 => Ok("SIGSEGV"),
12 => Ok("SIGSYS"),
13 => Ok("SIGPIPE"),
14 => Ok("SIGALRM"),
15 => Ok("SIGTERM"),
16 => Ok("SIGURG"),
17 => Ok("SIGSTOP"),
18 => Ok("SIGTSTP"),
19 => Ok("SIGCONT"),
20 => Ok("SIGCHLD"),
21 => Ok("SIGTTIN"),
22 => Ok("SIGTTOU"),
23 => Ok("SIGIO"),
24 => Ok("SIGXCPU"),
25 => Ok("SIGXFSZ"),
26 => Ok("SIGVTALRM"),
27 => Ok("SIGPROF"),
28 => Ok("SIGWINCH"),
29 => Ok("SIGINFO"),
30 => Ok("SIGUSR1"),
31 => Ok("SIGUSR2"),
32 => Ok("SIGTHR"),
_ => Err(type_error(format!("Invalid signal : {}", s))),
}
}
#[cfg(any(target_os = "android", target_os = "linux"))] #[cfg(any(target_os = "android", target_os = "linux"))]
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> { signal_dict!(
match s { |s| { format!("Invalid signal : {s}") },
"SIGHUP" => Ok(1), (1, "SIGHUP"),
"SIGINT" => Ok(2), (2, "SIGINT"),
"SIGQUIT" => Ok(3), (3, "SIGQUIT"),
"SIGILL" => Ok(4), (4, "SIGILL"),
"SIGTRAP" => Ok(5), (5, "SIGTRAP"),
"SIGIOT" => Ok(6), (6, "SIGABRT" | "SIGIOT"),
"SIGABRT" => Ok(6), (7, "SIGBUS"),
"SIGBUS" => Ok(7), (8, "SIGFPE"),
"SIGFPE" => Ok(8), (9, "SIGKILL"),
"SIGKILL" => Ok(9), (10, "SIGUSR1"),
"SIGUSR1" => Ok(10), (11, "SIGSEGV"),
"SIGSEGV" => Ok(11), (12, "SIGUSR2"),
"SIGUSR2" => Ok(12), (13, "SIGPIPE"),
"SIGPIPE" => Ok(13), (14, "SIGALRM"),
"SIGALRM" => Ok(14), (15, "SIGTERM"),
"SIGTERM" => Ok(15), (16, "SIGSTKFLT"),
"SIGSTKFLT" => Ok(16), (17, "SIGCHLD"),
"SIGCHLD" => Ok(17), (18, "SIGCONT"),
"SIGCONT" => Ok(18), (19, "SIGSTOP"),
"SIGSTOP" => Ok(19), (20, "SIGTSTP"),
"SIGTSTP" => Ok(20), (21, "SIGTTIN"),
"SIGTTIN" => Ok(21), (22, "SIGTTOU"),
"SIGTTOU" => Ok(22), (23, "SIGURG"),
"SIGURG" => Ok(23), (24, "SIGXCPU"),
"SIGXCPU" => Ok(24), (25, "SIGXFSZ"),
"SIGXFSZ" => Ok(25), (26, "SIGVTALRM"),
"SIGVTALRM" => Ok(26), (27, "SIGPROF"),
"SIGPROF" => Ok(27), (28, "SIGWINCH"),
"SIGWINCH" => Ok(28), (29, "SIGIO" | "SIGPOLL"),
"SIGIO" | "SIGPOLL" => Ok(29), (30, "SIGPWR"),
"SIGPWR" => Ok(30), (31, "SIGSYS" | "SIGUNUSED")
"SIGSYS" | "SIGUNUSED" => Ok(31), );
_ => Err(type_error(format!("Invalid signal : {s}"))),
}
}
#[cfg(any(target_os = "android", target_os = "linux"))]
pub fn signal_int_to_str(s: libc::c_int) -> Result<&'static str, AnyError> {
match s {
1 => Ok("SIGHUP"),
2 => Ok("SIGINT"),
3 => Ok("SIGQUIT"),
4 => Ok("SIGILL"),
5 => Ok("SIGTRAP"),
6 => Ok("SIGABRT"),
7 => Ok("SIGBUS"),
8 => Ok("SIGFPE"),
9 => Ok("SIGKILL"),
10 => Ok("SIGUSR1"),
11 => Ok("SIGSEGV"),
12 => Ok("SIGUSR2"),
13 => Ok("SIGPIPE"),
14 => Ok("SIGALRM"),
15 => Ok("SIGTERM"),
16 => Ok("SIGSTKFLT"),
17 => Ok("SIGCHLD"),
18 => Ok("SIGCONT"),
19 => Ok("SIGSTOP"),
20 => Ok("SIGTSTP"),
21 => Ok("SIGTTIN"),
22 => Ok("SIGTTOU"),
23 => Ok("SIGURG"),
24 => Ok("SIGXCPU"),
25 => Ok("SIGXFSZ"),
26 => Ok("SIGVTALRM"),
27 => Ok("SIGPROF"),
28 => Ok("SIGWINCH"),
29 => Ok("SIGIO"),
30 => Ok("SIGPWR"),
31 => Ok("SIGSYS"),
_ => Err(type_error(format!("Invalid signal : {s}"))),
}
}
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> { signal_dict!(
match s { |s| { format!("Invalid signal : {s}") },
"SIGHUP" => Ok(1), (1, "SIGHUP"),
"SIGINT" => Ok(2), (2, "SIGINT"),
"SIGQUIT" => Ok(3), (3, "SIGQUIT"),
"SIGILL" => Ok(4), (4, "SIGILL"),
"SIGTRAP" => Ok(5), (5, "SIGTRAP"),
"SIGIOT" => Ok(6), (6, "SIGABRT" | "SIGIOT"),
"SIGABRT" => Ok(6), (7, "SIGEMT"),
"SIGEMT" => Ok(7), (8, "SIGFPE"),
"SIGFPE" => Ok(8), (9, "SIGKILL"),
"SIGKILL" => Ok(9), (10, "SIGBUS"),
"SIGBUS" => Ok(10), (11, "SIGSEGV"),
"SIGSEGV" => Ok(11), (12, "SIGSYS"),
"SIGSYS" => Ok(12), (13, "SIGPIPE"),
"SIGPIPE" => Ok(13), (14, "SIGALRM"),
"SIGALRM" => Ok(14), (15, "SIGTERM"),
"SIGTERM" => Ok(15), (16, "SIGURG"),
"SIGURG" => Ok(16), (17, "SIGSTOP"),
"SIGSTOP" => Ok(17), (18, "SIGTSTP"),
"SIGTSTP" => Ok(18), (19, "SIGCONT"),
"SIGCONT" => Ok(19), (20, "SIGCHLD"),
"SIGCHLD" => Ok(20), (21, "SIGTTIN"),
"SIGTTIN" => Ok(21), (22, "SIGTTOU"),
"SIGTTOU" => Ok(22), (23, "SIGIO"),
"SIGIO" => Ok(23), (24, "SIGXCPU"),
"SIGXCPU" => Ok(24), (25, "SIGXFSZ"),
"SIGXFSZ" => Ok(25), (26, "SIGVTALRM"),
"SIGVTALRM" => Ok(26), (27, "SIGPROF"),
"SIGPROF" => Ok(27), (28, "SIGWINCH"),
"SIGWINCH" => Ok(28), (29, "SIGINFO"),
"SIGINFO" => Ok(29), (30, "SIGUSR1"),
"SIGUSR1" => Ok(30), (31, "SIGUSR2")
"SIGUSR2" => Ok(31), );
_ => Err(type_error(format!("Invalid signal: {s}"))),
}
}
#[cfg(target_os = "macos")]
pub fn signal_int_to_str(s: libc::c_int) -> Result<&'static str, AnyError> {
match s {
1 => Ok("SIGHUP"),
2 => Ok("SIGINT"),
3 => Ok("SIGQUIT"),
4 => Ok("SIGILL"),
5 => Ok("SIGTRAP"),
6 => Ok("SIGABRT"),
7 => Ok("SIGEMT"),
8 => Ok("SIGFPE"),
9 => Ok("SIGKILL"),
10 => Ok("SIGBUS"),
11 => Ok("SIGSEGV"),
12 => Ok("SIGSYS"),
13 => Ok("SIGPIPE"),
14 => Ok("SIGALRM"),
15 => Ok("SIGTERM"),
16 => Ok("SIGURG"),
17 => Ok("SIGSTOP"),
18 => Ok("SIGTSTP"),
19 => Ok("SIGCONT"),
20 => Ok("SIGCHLD"),
21 => Ok("SIGTTIN"),
22 => Ok("SIGTTOU"),
23 => Ok("SIGIO"),
24 => Ok("SIGXCPU"),
25 => Ok("SIGXFSZ"),
26 => Ok("SIGVTALRM"),
27 => Ok("SIGPROF"),
28 => Ok("SIGWINCH"),
29 => Ok("SIGINFO"),
30 => Ok("SIGUSR1"),
31 => Ok("SIGUSR2"),
_ => Err(type_error(format!("Invalid signal: {s}"))),
}
}
#[cfg(any(target_os = "solaris", target_os = "illumos"))] #[cfg(any(target_os = "solaris", target_os = "illumos"))]
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> { signal_dict!(
match s { |s| { format!("Invalid signal : {s}") },
"SIGHUP" => Ok(1), (1, "SIGHUP"),
"SIGINT" => Ok(2), (2, "SIGINT"),
"SIGQUIT" => Ok(3), (3, "SIGQUIT"),
"SIGILL" => Ok(4), (4, "SIGILL"),
"SIGTRAP" => Ok(5), (5, "SIGTRAP"),
"SIGIOT" => Ok(6), (6, "SIGABRT" | "SIGIOT"),
"SIGABRT" => Ok(6), (7, "SIGEMT"),
"SIGEMT" => Ok(7), (8, "SIGFPE"),
"SIGFPE" => Ok(8), (9, "SIGKILL"),
"SIGKILL" => Ok(9), (10, "SIGBUS"),
"SIGBUS" => Ok(10), (11, "SIGSEGV"),
"SIGSEGV" => Ok(11), (12, "SIGSYS"),
"SIGSYS" => Ok(12), (13, "SIGPIPE"),
"SIGPIPE" => Ok(13), (14, "SIGALRM"),
"SIGALRM" => Ok(14), (15, "SIGTERM"),
"SIGTERM" => Ok(15), (16, "SIGUSR1"),
"SIGUSR1" => Ok(16), (17, "SIGUSR2"),
"SIGUSR2" => Ok(17), (18, "SIGCHLD"),
"SIGCLD" => Ok(18), (19, "SIGPWR"),
"SIGCHLD" => Ok(18), (20, "SIGWINCH"),
"SIGPWR" => Ok(19), (21, "SIGURG"),
"SIGWINCH" => Ok(20), (22, "SIGPOLL"),
"SIGURG" => Ok(21), (23, "SIGSTOP"),
"SIGPOLL" => Ok(22), (24, "SIGTSTP"),
"SIGIO" => Ok(22), (25, "SIGCONT"),
"SIGSTOP" => Ok(23), (26, "SIGTTIN"),
"SIGTSTP" => Ok(24), (27, "SIGTTOU"),
"SIGCONT" => Ok(25), (28, "SIGVTALRM"),
"SIGTTIN" => Ok(26), (29, "SIGPROF"),
"SIGTTOU" => Ok(27), (30, "SIGXCPU"),
"SIGVTALRM" => Ok(28), (31, "SIGXFSZ"),
"SIGPROF" => Ok(29), (32, "SIGWAITING"),
"SIGXCPU" => Ok(30), (33, "SIGLWP"),
"SIGXFSZ" => Ok(31), (34, "SIGFREEZE"),
"SIGWAITING" => Ok(32), (35, "SIGTHAW"),
"SIGLWP" => Ok(33), (36, "SIGCANCEL"),
"SIGFREEZE" => Ok(34), (37, "SIGLOST"),
"SIGTHAW" => Ok(35), (38, "SIGXRES"),
"SIGCANCEL" => Ok(36), (39, "SIGJVM1"),
"SIGLOST" => Ok(37), (40, "SIGJVM2")
"SIGXRES" => Ok(38), );
"SIGJVM1" => Ok(39),
"SIGJVM2" => Ok(40),
_ => Err(type_error(format!("Invalid signal : {}", s))),
}
}
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
pub fn signal_int_to_str(s: libc::c_int) -> Result<&'static str, AnyError> {
match s {
1 => Ok("SIGHUP"),
2 => Ok("SIGINT"),
3 => Ok("SIGQUIT"),
4 => Ok("SIGILL"),
5 => Ok("SIGTRAP"),
6 => Ok("SIGABRT"),
7 => Ok("SIGEMT"),
8 => Ok("SIGFPE"),
9 => Ok("SIGKILL"),
10 => Ok("SIGBUS"),
11 => Ok("SIGSEGV"),
12 => Ok("SIGSYS"),
13 => Ok("SIGPIPE"),
14 => Ok("SIGALRM"),
15 => Ok("SIGTERM"),
16 => Ok("SIGUSR1"),
17 => Ok("SIGUSR2"),
18 => Ok("SIGCHLD"),
19 => Ok("SIGPWR"),
20 => Ok("SIGWINCH"),
21 => Ok("SIGURG"),
22 => Ok("SIGPOLL"),
23 => Ok("SIGSTOP"),
24 => Ok("SIGTSTP"),
25 => Ok("SIGCONT"),
26 => Ok("SIGTTIN"),
27 => Ok("SIGTTOU"),
28 => Ok("SIGVTALRM"),
29 => Ok("SIGPROF"),
30 => Ok("SIGXCPU"),
31 => Ok("SIGXFSZ"),
32 => Ok("SIGWAITING"),
33 => Ok("SIGLWP"),
34 => Ok("SIGFREEZE"),
35 => Ok("SIGTHAW"),
36 => Ok("SIGCANCEL"),
37 => Ok("SIGLOST"),
38 => Ok("SIGXRES"),
39 => Ok("SIGJVM1"),
40 => Ok("SIGJVM2"),
_ => Err(type_error(format!("Invalid signal : {}", s))),
}
}
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> { signal_dict!(
match s { |_| { "Windows only supports ctrl-c (SIGINT) and ctrl-break (SIGBREAK)." },
"SIGINT" => Ok(2), (2, "SIGINT"),
"SIGBREAK" => Ok(21), (21, "SIGBREAK")
_ => Err(type_error( );
"Windows only supports ctrl-c (SIGINT) and ctrl-break (SIGBREAK).",
)),
}
}
#[cfg(target_os = "windows")]
pub fn signal_int_to_str(s: libc::c_int) -> Result<&'static str, AnyError> {
match s {
2 => Ok("SIGINT"),
21 => Ok("SIGBREAK"),
_ => Err(type_error(
"Windows only supports ctrl-c (SIGINT) and ctrl-break (SIGBREAK).",
)),
}
}
#[cfg(unix)] #[cfg(unix)]
#[op2(fast)] #[op2(fast)]

View file

@ -1,6 +1,11 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, loadTestLibrary } from "./common.js"; import {
assert,
assertEquals,
assertThrows,
loadTestLibrary,
} from "./common.js";
const object = loadTestLibrary(); const object = loadTestLibrary();
@ -12,4 +17,22 @@ Deno.test("napi object", function () {
const r1 = object.test_object_get(r); const r1 = object.test_object_get(r);
assert(r === r1); assert(r === r1);
const r2 = object.test_object_attr_property(r);
assert(r === r2);
assertThrows(
() => {
r2.self = "2";
},
Error,
"Cannot assign to read only property 'self' of object '#<Object>'",
);
assertThrows(
() => {
r2.method = () => {};
},
Error,
"Cannot assign to read only property 'method' of object '#<Object>'",
);
}); });

View file

@ -40,10 +40,39 @@ extern "C" fn test_object_get(
obj obj
} }
extern "C" fn test_object_attr_property(
env: napi_env,
info: napi_callback_info,
) -> napi_value {
let (args, argc, _) = napi_get_callback_info!(env, info, 1);
assert_eq!(argc, 1);
let obj = args[0];
let mut property = napi_new_property!(env, "self", test_object_new);
property.attributes = PropertyAttributes::enumerable;
property.method = None;
property.value = obj;
let mut method_property = napi_new_property!(env, "method", test_object_new);
method_property.attributes = PropertyAttributes::enumerable;
let properties = &[property, method_property];
assert_napi_ok!(napi_define_properties(
env,
obj,
properties.len(),
properties.as_ptr()
));
obj
}
pub fn init(env: napi_env, exports: napi_value) { pub fn init(env: napi_env, exports: napi_value) {
let properties = &[ let properties = &[
napi_new_property!(env, "test_object_new", test_object_new), napi_new_property!(env, "test_object_new", test_object_new),
napi_new_property!(env, "test_object_get", test_object_get), napi_new_property!(env, "test_object_get", test_object_get),
napi_new_property!(
env,
"test_object_attr_property",
test_object_attr_property
),
]; ];
assert_napi_ok!(napi_define_properties( assert_napi_ok!(napi_define_properties(