Skip to content

Commit

Permalink
feat: Add ModuleRequest (denoland#623)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju authored Feb 14, 2021
1 parent 78fa073 commit ec54f28
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2048,6 +2048,10 @@ const v8::String* v8__Module__GetModuleRequest(const v8::Module& self, int i) {
return local_to_ptr(self.GetModuleRequest(i));
}

const v8::FixedArray* v8__Module__GetModuleRequests(const v8::Module& self) {
return local_to_ptr(self.GetModuleRequests());
}

void v8__Module__GetModuleRequestLocation(const v8::Module& self, int i,
v8::Location* out) {
*out = self.GetModuleRequestLocation(i);
Expand Down Expand Up @@ -2110,6 +2114,18 @@ MaybeBool v8__Module__SetSyntheticModuleExport(const v8::Module& self,
isolate, ptr_to_local(export_name), ptr_to_local(export_value)));
}

const v8::String* v8__ModuleRequest__GetSpecifier(const v8::ModuleRequest& self) {
return local_to_ptr(self.GetSpecifier());
}

int v8__ModuleRequest__GetSourceOffset(const v8::ModuleRequest& self) {
return self.GetSourceOffset();
}

const v8::FixedArray* v8__ModuleRequest__GetImportAssertions(const v8::ModuleRequest& self) {
return local_to_ptr(self.GetImportAssertions());
}

struct WasmStreamingSharedPtr {
std::shared_ptr<v8::WasmStreaming> inner;
};
Expand Down
12 changes: 12 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ impl_from! { AccessorSignature for Data }
impl_from! { Context for Data }
impl_from! { Message for Data }
impl_from! { Module for Data }
impl_from! { ModuleRequest for Data }
impl_from! { PrimitiveArray for Data }
impl_from! { Private for Data }
impl_from! { Script for Data }
Expand Down Expand Up @@ -367,6 +368,17 @@ impl_hash! { for FixedArray }
impl_partial_eq! { Data for FixedArray use identity }
impl_partial_eq! { FixedArray for FixedArray use identity }

#[repr(C)]
#[derive(Debug)]
pub struct ModuleRequest(Opaque);

impl_deref! { Data for ModuleRequest }
impl_from! { Data for ModuleRequest }
impl_eq! { for ModuleRequest }
impl_hash! { for ModuleRequest }
impl_partial_eq! { Data for ModuleRequest use identity }
impl_partial_eq! { ModuleRequest for ModuleRequest use identity }

/// An array to hold Primitive values. This is used by the embedder to
/// pass host defined options to the ScriptOptions during compilation.
///
Expand Down
44 changes: 44 additions & 0 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::HandleScope;
use crate::Isolate;
use crate::Local;
use crate::Module;
use crate::ModuleRequest;
use crate::String;
use crate::Value;

Expand Down Expand Up @@ -138,6 +139,7 @@ extern "C" {
fn v8__Module__GetModuleRequestsLength(this: *const Module) -> int;
fn v8__Module__GetModuleRequest(this: *const Module, i: int)
-> *const String;
fn v8__Module__GetModuleRequests(this: *const Module) -> *const FixedArray;
fn v8__Module__GetModuleRequestLocation(
this: *const Module,
i: int,
Expand Down Expand Up @@ -172,6 +174,13 @@ extern "C" {
) -> MaybeBool;
fn v8__Location__GetLineNumber(this: *const Location) -> int;
fn v8__Location__GetColumnNumber(this: *const Location) -> int;
fn v8__ModuleRequest__GetSpecifier(
this: *const ModuleRequest,
) -> *const String;
fn v8__ModuleRequest__GetSourceOffset(this: *const ModuleRequest) -> int;
fn v8__ModuleRequest__GetImportAssertions(
this: *const ModuleRequest,
) -> *const FixedArray;
}

/// A location in JavaScript source.
Expand Down Expand Up @@ -236,6 +245,11 @@ impl Module {
.unwrap()
}

/// Returns the ModuleRequests for this module.
pub fn get_module_requests(&self) -> Local<FixedArray> {
unsafe { Local::from_raw(v8__Module__GetModuleRequests(self)) }.unwrap()
}

/// Returns the source location (line number and column number) of the ith
/// module specifier's first occurrence in this module.
pub fn get_module_request_location(&self, i: usize) -> Location {
Expand Down Expand Up @@ -387,3 +401,33 @@ impl Hash for Module {
state.write_i32(self.get_identity_hash());
}
}

impl ModuleRequest {
/// Returns the module specifier for this ModuleRequest.
pub fn get_specifier(&self) -> Local<String> {
unsafe { Local::from_raw(v8__ModuleRequest__GetSpecifier(self)) }.unwrap()
}

/// Returns the source code offset of this module request.
/// Use Module::SourceOffsetToLocation to convert this to line/column numbers.
pub fn get_source_offset(&self) -> int {
unsafe { v8__ModuleRequest__GetSourceOffset(self) }
}

/// Contains the import assertions for this request in the form:
/// [key1, value1, source_offset1, key2, value2, source_offset2, ...].
/// The keys and values are of type v8::String, and the source offsets are of
/// type Int32. Use Module::SourceOffsetToLocation to convert the source
/// offsets to Locations with line/column numbers.
///
/// All assertions present in the module request will be supplied in this
/// list, regardless of whether they are supported by the host. Per
/// https://tc39.es/proposal-import-assertions/#sec-hostgetsupportedimportassertions,
/// hosts are expected to ignore assertions that they do not support (as
/// opposed to, for example, triggering an error if an unsupported assertion is
/// present).
pub fn get_import_assertions(&self) -> Local<FixedArray> {
unsafe { Local::from_raw(v8__ModuleRequest__GetImportAssertions(self)) }
.unwrap()
}
}
13 changes: 12 additions & 1 deletion tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1952,7 +1952,18 @@ fn module_instantiation_failures1() {

let module = v8::script_compiler::compile_module(scope, source).unwrap();
assert_eq!(v8::ModuleStatus::Uninstantiated, module.get_status());
assert_eq!(2, module.get_module_requests_length());
let module_requests = module.get_module_requests();
assert_eq!(2, module_requests.length());
let mr1 = v8::Local::<v8::ModuleRequest>::try_from(
module_requests.get(scope, 0).unwrap(),
)
.unwrap();
assert_eq!(0, mr1.get_import_assertions().length());
let mr2 = v8::Local::<v8::ModuleRequest>::try_from(
module_requests.get(scope, 1).unwrap(),
)
.unwrap();
assert_eq!(0, mr2.get_import_assertions().length());
assert!(module.script_id().is_some());

assert_eq!(
Expand Down

0 comments on commit ec54f28

Please sign in to comment.