FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
windows/runtime/src/napi_engine/module_natives.rs at main · NativeScript/windows · GitHub
NativeScript
windows
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
windows
/
runtime
/
src
/
napi_engine
/
module_natives.rs
Copy path
More file actions
More file actions
Latest commit
History
History
History
133 lines (122 loc) · 6.1 KB
Breadcrumbs
windows
/
runtime
/
src
/
napi_engine
/
module_natives.rs
Copy path
File metadata and controls
133 lines (122 loc) · 6.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//! CommonJS natives for webpack `target: 'node'` bundles: NativeScript apps are built expecting
//! `require`/`module`/`exports`/`__dirname`/`__filename` as globals (Node normally provides these
//! via its module wrapper; this runtime is not Node). This module provides the two natives that
//! back that shim — `__nsResolveModulePath` and `__nsReadTextFile` — plus a read-only
//! `__nsAppRoot` string (the classic rusty_v8 runtime supplies the equivalent JS shim from
//! `global_fns::HELPER_SOURCE`). The JS-side `require`/`module`/`exports` shim itself lives in
//! `packages/common/src/prelude.rs` so it runs unchanged on every napi engine.
//!
//! Without this, every chunk a webpack "node" build produces (`runtime.js`/`vendor.js`/the app
//! bundle) throws `ReferenceError: require/module/exports is not defined` on evaluation — silently,
//! since a top-level throw here is caught by the host and never reaches the JS error/console
//! surfaces a real app would show. See [[project_engine_framework_parity]] for how this was found.
use
std
::
path
::
{
Path
,
PathBuf
}
;
use
napi
::
{
CallContext
,
Env
,
JsUnknown
,
ValueType
}
;
use
crate
::
napi_engine
::
value
::
as_unknown
;
fn
arg_string
(
ctx
:
&
CallContext
,
index
:
usize
)
-> napi
::
Result
<
Option
<
String
>
>
{
if
ctx
.
length
<= index
{
return
Ok
(
None
)
;
}
let
v = ctx
.
get
::
<
JsUnknown
>
(
index
)
?
;
if
matches
!
(
v
.
get_type
(
)
,
Ok
(
ValueType
::
Null
)
|
Ok
(
ValueType
::
Undefined
)
)
{
return
Ok
(
None
)
;
}
let
s = v
.
coerce_to_string
(
)
?
.
into_utf8
(
)
?
.
as_str
(
)
?
.
to_owned
(
)
;
Ok
(
Some
(
s
)
)
}
/// Install `__nsAppRoot` (read-only string), `__nsReadTextFile`, `__nsResolveModulePath` —
/// install-if-missing, matching the rest of `install_globals`.
pub
fn
install_module_natives
(
env
:
&
Env
,
app_root
:
&
str
)
-> napi
::
Result
<
(
)
>
{
let
mut
global = env
.
get_global
(
)
?
;
let
has_app_root =
matches
!
(
global
.
get_named_property
::
<
JsUnknown
>
(
"__nsAppRoot"
)
.
and_then
(
|v| v
.
get_type
(
)
)
,
Ok
(
ValueType
::
String
)
)
;
if
!has_app_root
{
global
.
set_named_property
(
"__nsAppRoot"
,
env
.
create_string
(
app_root
)
?
)
?
;
}
let
read_text_file =
env
.
create_function_from_closure
(
"__nsReadTextFile"
,
|
ctx
:
CallContext
|
{
let
env =
&
ctx
.
env
;
let
Some
(
path
)
=
arg_string
(
&
ctx
,
0
)
?
else
{
return
Err
(
napi
::
Error
::
from_reason
(
"__nsReadTextFile: path is null, undefined, or missing"
,
)
)
;
}
;
if
path
.
is_empty
(
)
{
return
Err
(
napi
::
Error
::
from_reason
(
"__nsReadTextFile: path is empty"
)
)
;
}
if
let
Some
(
content
)
=
crate
::
source_protect
::
read_text
(
&
path
)
{
return
Ok
(
env
.
create_string
(
&
content
)
?
)
;
}
match
std
::
fs
::
read_to_string
(
Path
::
new
(
&
path
)
)
{
Ok
(
content
)
=>
Ok
(
env
.
create_string
(
&
content
)
?
)
,
Err
(
err
)
=>
Err
(
napi
::
Error
::
from_reason
(
format
!
(
"Failed to read module file: {err}"
)
)
)
,
}
}
)
?
;
global
.
set_named_property
(
"__nsReadTextFile"
,
read_text_file
)
?
;
let
resolve_module_path =
env
.
create_function_from_closure
(
"__nsResolveModulePath"
,
|
ctx
:
CallContext
|
{
let
env =
&
ctx
.
env
;
let
Some
(
specifier
)
=
arg_string
(
&
ctx
,
0
)
?
else
{
return
Err
(
napi
::
Error
::
from_reason
(
"__nsResolveModulePath: module specifier is null, undefined, or missing"
,
)
)
;
}
;
if
specifier
.
is_empty
(
)
{
return
Err
(
napi
::
Error
::
from_reason
(
"__nsResolveModulePath: module specifier is empty"
,
)
)
;
}
let
parent_path =
arg_string
(
&
ctx
,
1
)
?
;
let
app_root =
arg_string
(
&
ctx
,
2
)
?
.
unwrap_or_default
(
)
;
let
mut
candidate =
if
specifier
.
starts_with
(
"./"
)
|| specifier
.
starts_with
(
"../"
)
{
let
parent = parent_path
.
map
(
|v|
crate
::
normalize_js_path
(
&
v
)
)
.
unwrap_or_else
(
|| std
::
env
::
current_dir
(
)
.
unwrap_or_else
(
|_|
PathBuf
::
from
(
"."
)
)
)
;
// A packed (virtual, no-plaintext-on-disk) referrer never satisfies `.is_file()`,
// so also check the sealed bundle's table — otherwise a relative require() from a
// bundle-only file would wrongly treat the referrer as the base dir, not its parent.
// A referrer that doesn't exist but names a file (the `app/bundle.js` fallback the
// top-level `require` uses when the entry script is not a bundle) is a file too.
let
base =
if
parent
.
is_file
(
)
||
crate
::
source_protect
::
contains
(
&
parent
.
to_string_lossy
(
)
)
||
(
!parent
.
exists
(
)
&& parent
.
extension
(
)
.
is_some
(
)
)
{
parent
.
parent
(
)
.
map
(
Path
::
to_path_buf
)
.
unwrap_or
(
parent
)
}
else
{
parent
}
;
base
.
join
(
&
specifier
)
}
else
{
let
direct =
crate
::
normalize_js_path
(
&
specifier
)
;
if
direct
.
is_absolute
(
)
{
direct
}
else
{
let
app_base =
if
app_root
.
is_empty
(
)
{
std
::
env
::
current_dir
(
)
.
unwrap_or_else
(
|_|
PathBuf
::
from
(
"."
)
)
}
else
{
let
lower =
PathBuf
::
from
(
&
app_root
)
.
join
(
"app"
)
;
if
lower
.
exists
(
)
{
lower
}
else
{
PathBuf
::
from
(
&
app_root
)
.
join
(
"App"
)
}
}
;
app_base
.
join
(
direct
)
}
}
;
candidate =
crate
::
try_resolve_with_known_extensions
(
candidate
)
;
let
resolved = candidate
.
canonicalize
(
)
.
unwrap_or
(
candidate
)
;
match
resolved
.
to_str
(
)
{
Some
(
p
)
=>
Ok
(
as_unknown
(
env
,
env
.
create_string
(
p
)
?
)
)
,
None
=>
Ok
(
as_unknown
(
env
,
env
.
get_null
(
)
?
)
)
,
}
}
)
?
;
global
.
set_named_property
(
"__nsResolveModulePath"
,
resolve_module_path
)
?
;
Ok
(
(
)
)
}
Back
|
FazBrowse Home
|
New Git URL