0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-13 09:33:02 -05:00

feat: Add Source Maps APIs (#1514)

This commit adds bindings for:

- v8::UnboundScript::get_source_mapping_url
- v8::UnboundScript::get_source_url
- v8::UnboundModuleScript::get_source_mapping_url
- v8::UnboundModuleScript::get_source_url
This commit is contained in:
Bartek Iwańczuk 2024-07-04 23:13:11 +01:00 committed by GitHub
parent 8df8a1a7cb
commit 49b92c1e76
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 293 additions and 2 deletions

View file

@ -2521,12 +2521,33 @@ v8::ScriptCompiler::CachedData* v8__UnboundScript__CreateCodeCache(
return v8::ScriptCompiler::CreateCodeCache(ptr_to_local(&unbound_script));
}
v8::Value* v8__UnboundScript__GetSourceMappingURL(
const v8::UnboundScript& unbound_script) {
return local_to_ptr(ptr_to_local(&unbound_script)->GetSourceMappingURL());
}
v8::Value* v8__UnboundScript__GetSourceURL(
const v8::UnboundScript& unbound_script) {
return local_to_ptr(ptr_to_local(&unbound_script)->GetSourceURL());
}
v8::ScriptCompiler::CachedData* v8__UnboundModuleScript__CreateCodeCache(
const v8::UnboundModuleScript& unbound_module_script) {
return v8::ScriptCompiler::CreateCodeCache(
ptr_to_local(&unbound_module_script));
}
v8::Value* v8__UnboundModuleScript__GetSourceMappingURL(
const v8::UnboundModuleScript& unbound_module_script) {
return local_to_ptr(
ptr_to_local(&unbound_module_script)->GetSourceMappingURL());
}
v8::Value* v8__UnboundModuleScript__GetSourceURL(
const v8::UnboundModuleScript& unbound_module_script) {
return local_to_ptr(ptr_to_local(&unbound_module_script)->GetSourceURL());
}
v8::ScriptCompiler::CachedData* v8__Function__CreateCodeCache(
const v8::Function& self) {
return v8::ScriptCompiler::CreateCodeCacheForFunction(ptr_to_local(&self));

View file

@ -1,11 +1,22 @@
use crate::CachedData;
use crate::HandleScope;
use crate::Local;
use crate::UnboundModuleScript;
use crate::UniqueRef;
use crate::Value;
extern "C" {
fn v8__UnboundModuleScript__CreateCodeCache(
script: *const UnboundModuleScript,
) -> *mut CachedData<'static>;
fn v8__UnboundModuleScript__GetSourceMappingURL(
script: *const UnboundModuleScript,
) -> *const Value;
fn v8__UnboundModuleScript__GetSourceURL(
script: *const UnboundModuleScript,
) -> *const Value;
}
impl UnboundModuleScript {
@ -25,4 +36,26 @@ impl UnboundModuleScript {
}
code_cache
}
pub fn get_source_mapping_url<'s>(
&self,
scope: &mut HandleScope<'s>,
) -> Local<'s, Value> {
unsafe {
scope
.cast_local(|_| v8__UnboundModuleScript__GetSourceMappingURL(self))
.unwrap()
}
}
pub fn get_source_url<'s>(
&self,
scope: &mut HandleScope<'s>,
) -> Local<'s, Value> {
unsafe {
scope
.cast_local(|_| v8__UnboundModuleScript__GetSourceURL(self))
.unwrap()
}
}
}

View file

@ -1,8 +1,10 @@
use crate::CachedData;
use crate::HandleScope;
use crate::Local;
use crate::Script;
use crate::UnboundScript;
use crate::{HandleScope, UniqueRef};
use crate::UniqueRef;
use crate::Value;
extern "C" {
fn v8__UnboundScript__BindToCurrentContext(
@ -11,6 +13,14 @@ extern "C" {
fn v8__UnboundScript__CreateCodeCache(
script: *const UnboundScript,
) -> *mut CachedData<'static>;
fn v8__UnboundScript__GetSourceMappingURL(
script: *const UnboundScript,
) -> *const Value;
fn v8__UnboundScript__GetSourceURL(
script: *const UnboundScript,
) -> *const Value;
}
impl UnboundScript {
@ -42,4 +52,28 @@ impl UnboundScript {
}
code_cache
}
#[inline(always)]
pub fn get_source_mapping_url<'s>(
&self,
scope: &mut HandleScope<'s>,
) -> Local<'s, Value> {
unsafe {
scope
.cast_local(|_| v8__UnboundScript__GetSourceMappingURL(self))
.unwrap()
}
}
#[inline(always)]
pub fn get_source_url<'s>(
&self,
scope: &mut HandleScope<'s>,
) -> Local<'s, Value> {
unsafe {
scope
.cast_local(|_| v8__UnboundScript__GetSourceURL(self))
.unwrap()
}
}
}

View file

@ -8595,7 +8595,11 @@ fn unbound_script_conversion() {
let unbound_script = {
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);
let source = v8::String::new(scope, "'Hello ' + value").unwrap();
let source = v8::String::new(
scope,
"'Hello ' + value\n//# sourceMappingURL=foo.js.map",
)
.unwrap();
let script = v8::Script::compile(scope, source, None).unwrap();
script.get_unbound_script(scope)
};
@ -8615,6 +8619,205 @@ fn unbound_script_conversion() {
}
}
#[test]
fn get_source_mapping_from_comment() {
let _setup_guard = setup::parallel_test();
let isolate = &mut v8::Isolate::new(Default::default());
let scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);
// TODO(bartlomieju): add a shorter version of `v8::ScriptOrigin`
let resource_name =
v8::String::new(scope, "http://www.foo.com/foo.js").unwrap();
let resource_line_offset = 0;
let resource_column_offset = 0;
let resource_is_shared_cross_origin = false;
let script_id = -1;
let source_map_url = v8::undefined(scope);
let resource_is_opaque = false;
let is_wasm = false;
let is_module = false;
let script_origin = v8::ScriptOrigin::new(
scope,
resource_name.into(),
resource_line_offset,
resource_column_offset,
resource_is_shared_cross_origin,
script_id,
source_map_url.into(),
resource_is_opaque,
is_wasm,
is_module,
);
let code =
v8::String::new(scope, "var foo;\n//# sourceMappingURL=foo.js.map")
.unwrap();
let mut source = v8::script_compiler::Source::new(code, Some(&script_origin));
let script = v8::script_compiler::compile(
scope,
&mut source,
v8::script_compiler::CompileOptions::EagerCompile,
v8::script_compiler::NoCacheReason::NoReason,
)
.unwrap();
let source_mapping_url = script
.get_unbound_script(scope)
.get_source_mapping_url(scope)
.to_rust_string_lossy(scope);
assert_eq!("foo.js.map", source_mapping_url)
}
#[test]
fn origin_source_map_overrides_source_mapping_url_comment() {
let _setup_guard = setup::parallel_test();
let isolate = &mut v8::Isolate::new(Default::default());
let scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);
let expected_source_map_url = "http://override/foo.js.map";
// TODO(bartlomieju): add a shorter version of `v8::ScriptOrigin`
let resource_name =
v8::String::new(scope, "http://www.foo.com/foo.js").unwrap();
let resource_line_offset = 13;
let resource_column_offset = 0;
let resource_is_shared_cross_origin = false;
let script_id = -1;
let source_map_url = v8::String::new(scope, expected_source_map_url).unwrap();
let resource_is_opaque = false;
let is_wasm = false;
let is_module = false;
let script_origin = v8::ScriptOrigin::new(
scope,
resource_name.into(),
resource_line_offset,
resource_column_offset,
resource_is_shared_cross_origin,
script_id,
source_map_url.into(),
resource_is_opaque,
is_wasm,
is_module,
);
let code =
v8::String::new(scope, "var foo;\n//# sourceMappingURL=foo.js.map")
.unwrap();
let mut source = v8::script_compiler::Source::new(code, Some(&script_origin));
let script = v8::script_compiler::compile(
scope,
&mut source,
v8::script_compiler::CompileOptions::EagerCompile,
v8::script_compiler::NoCacheReason::NoReason,
)
.unwrap();
let source_mapping_url = script
.get_unbound_script(scope)
.get_source_mapping_url(scope)
.to_rust_string_lossy(scope);
assert_eq!(expected_source_map_url, source_mapping_url)
}
#[test]
fn ignore_origin_source_map_empty_string() {
let _setup_guard = setup::parallel_test();
let isolate = &mut v8::Isolate::new(Default::default());
let scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);
// TODO(bartlomieju): add a shorter version of `v8::ScriptOrigin`
let resource_name =
v8::String::new(scope, "http://www.foo.com/foo.js").unwrap();
let resource_line_offset = 0;
let resource_column_offset = 0;
let resource_is_shared_cross_origin = false;
let script_id = -1;
let source_map_url = v8::String::new(scope, "").unwrap();
let resource_is_opaque = false;
let is_wasm = false;
let is_module = false;
let script_origin = v8::ScriptOrigin::new(
scope,
resource_name.into(),
resource_line_offset,
resource_column_offset,
resource_is_shared_cross_origin,
script_id,
source_map_url.into(),
resource_is_opaque,
is_wasm,
is_module,
);
let code =
v8::String::new(scope, "var foo;\n//# sourceMappingURL=foo.js.map")
.unwrap();
let mut source = v8::script_compiler::Source::new(code, Some(&script_origin));
let script = v8::script_compiler::compile(
scope,
&mut source,
v8::script_compiler::CompileOptions::EagerCompile,
v8::script_compiler::NoCacheReason::NoReason,
)
.unwrap();
let source_mapping_url = script
.get_unbound_script(scope)
.get_source_mapping_url(scope)
.to_rust_string_lossy(scope);
assert_eq!("foo.js.map", source_mapping_url)
}
#[test]
fn no_source_map_comment() {
let _setup_guard = setup::parallel_test();
let isolate = &mut v8::Isolate::new(Default::default());
let scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);
// TODO(bartlomieju): add a shorter version of `v8::ScriptOrigin`
let resource_name =
v8::String::new(scope, "http://www.foo.com/foo.js").unwrap();
let resource_line_offset = 0;
let resource_column_offset = 0;
let resource_is_shared_cross_origin = false;
let script_id = -1;
let source_map_url = v8::undefined(scope);
let resource_is_opaque = false;
let is_wasm = false;
let is_module = false;
let script_origin = v8::ScriptOrigin::new(
scope,
resource_name.into(),
resource_line_offset,
resource_column_offset,
resource_is_shared_cross_origin,
script_id,
source_map_url.into(),
resource_is_opaque,
is_wasm,
is_module,
);
let code = v8::String::new(scope, "var foo;\n").unwrap();
let mut source = v8::script_compiler::Source::new(code, Some(&script_origin));
let script = v8::script_compiler::compile(
scope,
&mut source,
v8::script_compiler::CompileOptions::EagerCompile,
v8::script_compiler::NoCacheReason::NoReason,
)
.unwrap();
let source_mapping_url = script
.get_unbound_script(scope)
.get_source_mapping_url(scope)
.to_rust_string_lossy(scope);
assert_eq!("undefined", source_mapping_url)
}
#[test]
fn ept_torture_test() {
let _setup_guard = setup::parallel_test();