Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add import.meta.main #1835

Merged
merged 9 commits into from Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Empty file added js/lib.import_meta.d.ts
Empty file.
2 changes: 1 addition & 1 deletion js/lib.web_assembly.d.ts
Expand Up @@ -162,7 +162,7 @@ declare namespace WebAssembly {
}
}

// TODO Move ImportMeta intos its own lib.import_meta.d.ts file?
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
interface ImportMeta {
url: string;
main: boolean;
}
10 changes: 7 additions & 3 deletions libdeno/binding.cc
Expand Up @@ -265,7 +265,8 @@ v8::ScriptOrigin ModuleOrigin(v8::Isolate* isolate,
v8::True(isolate));
}

deno_mod DenoIsolate::RegisterModule(const char* name, const char* source) {
deno_mod DenoIsolate::RegisterModule(bool main, const char* name,
const char* source) {
v8::Isolate::Scope isolate_scope(isolate_);
v8::Locker locker(isolate_);
v8::HandleScope handle_scope(isolate_);
Expand Down Expand Up @@ -300,8 +301,9 @@ deno_mod DenoIsolate::RegisterModule(const char* name, const char* source) {
import_specifiers.push_back(*specifier_utf8);
}

mods_.emplace(std::piecewise_construct, std::make_tuple(id),
std::make_tuple(isolate_, module, name, import_specifiers));
mods_.emplace(
std::piecewise_construct, std::make_tuple(id),
std::make_tuple(isolate_, module, main, name, import_specifiers));
mods_by_name_[name] = id;

return id;
Expand Down Expand Up @@ -519,8 +521,10 @@ void HostInitializeImportMetaObjectCallback(v8::Local<v8::Context> context,
auto* info = d->GetModuleInfo(id);

const char* url = info->name.c_str();
const bool main = info->main;

meta->CreateDataProperty(context, v8_str("url"), v8_str(url)).ToChecked();
meta->CreateDataProperty(context, v8_str("main"), v8_bool(main)).ToChecked();
}

void DenoIsolate::AddIsolate(v8::Isolate* isolate) {
Expand Down
2 changes: 1 addition & 1 deletion libdeno/deno.h
Expand Up @@ -83,7 +83,7 @@ void deno_terminate_execution(Deno* d);
typedef int deno_mod;

// Returns zero on error - check deno_last_exception().
deno_mod deno_mod_new(Deno* d, const char* name, const char* source);
deno_mod deno_mod_new(Deno* d, bool main, const char* name, const char* source);

size_t deno_mod_imports_len(Deno* d, deno_mod id);

Expand Down
7 changes: 4 additions & 3 deletions libdeno/internal.h
Expand Up @@ -13,13 +13,14 @@
namespace deno {

struct ModuleInfo {
bool main;
std::string name;
v8::Persistent<v8::Module> handle;
std::vector<std::string> import_specifiers;

ModuleInfo(v8::Isolate* isolate, v8::Local<v8::Module> module,
ModuleInfo(v8::Isolate* isolate, v8::Local<v8::Module> module, bool main_,
const char* name_, std::vector<std::string> import_specifiers_)
: name(name_), import_specifiers(import_specifiers_) {
: main(main_), name(name_), import_specifiers(import_specifiers_) {
handle.Reset(isolate, module);
}
};
Expand Down Expand Up @@ -61,7 +62,7 @@ class DenoIsolate {

void AddIsolate(v8::Isolate* isolate);

deno_mod RegisterModule(const char* name, const char* source);
deno_mod RegisterModule(bool main, const char* name, const char* source);
v8::Local<v8::Object> GetBuiltinModules();
void ClearModules();

Expand Down
6 changes: 3 additions & 3 deletions libdeno/modules.cc
Expand Up @@ -88,7 +88,7 @@ v8::MaybeLocal<v8::Module> ResolveCallback(Local<Context> context,
id = it->second;
} else {
std::string src = BuiltinModuleSrc(context, specifier);
id = d->RegisterModule(req_str.c_str(), src.c_str());
id = d->RegisterModule(false, req_str.c_str(), src.c_str());
}
} else {
id = d->resolve_cb_(d->user_data_, req_str.c_str(), referrer_id);
Expand Down Expand Up @@ -116,10 +116,10 @@ v8::MaybeLocal<v8::Module> ResolveCallback(Local<Context> context,

extern "C" {

deno_mod deno_mod_new(Deno* d_, const char* name_cstr,
deno_mod deno_mod_new(Deno* d_, bool main, const char* name_cstr,
const char* source_cstr) {
auto* d = unwrap(d_);
return d->RegisterModule(name_cstr, source_cstr);
return d->RegisterModule(main, name_cstr, source_cstr);
}

const char* deno_mod_name(Deno* d_, deno_mod id) {
Expand Down
47 changes: 38 additions & 9 deletions libdeno/modules_test.cc
Expand Up @@ -14,15 +14,15 @@ TEST(ModulesTest, Resolution) {
Deno* d = deno_new(deno_config{0, empty, empty, recv_cb});
EXPECT_EQ(0, exec_count);

static deno_mod a = deno_mod_new(d, "a.js",
static deno_mod a = deno_mod_new(d, true, "a.js",
"import { b } from 'b.js'\n"
"if (b() != 'b') throw Error();\n"
"libdeno.send(new Uint8Array([4]));");
EXPECT_NE(a, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));

const char* b_src = "export function b() { return 'b' }";
static deno_mod b = deno_mod_new(d, "b.js", b_src);
static deno_mod b = deno_mod_new(d, false, "b.js", b_src);
EXPECT_NE(b, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));

Expand Down Expand Up @@ -72,7 +72,7 @@ TEST(ModulesTest, BuiltinModules) {
EXPECT_EQ(nullptr, deno_last_exception(d));

static deno_mod a =
deno_mod_new(d, "a.js",
deno_mod_new(d, true, "a.js",
"import { b } from 'b.js'\n"
"import * as deno from 'deno'\n"
"if (b() != 'b') throw Error('b');\n"
Expand All @@ -82,7 +82,7 @@ TEST(ModulesTest, BuiltinModules) {
EXPECT_EQ(nullptr, deno_last_exception(d));

const char* b_src = "export function b() { return 'b' }";
static deno_mod b = deno_mod_new(d, "b.js", b_src);
static deno_mod b = deno_mod_new(d, false, "b.js", b_src);
EXPECT_NE(b, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));

Expand Down Expand Up @@ -134,7 +134,7 @@ TEST(ModulesTest, BuiltinModules2) {
EXPECT_EQ(nullptr, deno_last_exception(d));

static deno_mod a =
deno_mod_new(d, "a.js",
deno_mod_new(d, true, "a.js",
"import * as b1 from 'builtin1'\n"
"import * as b2 from 'builtin2'\n"
"if (b1.foo != 'bar') throw Error('bad1');\n"
Expand Down Expand Up @@ -168,7 +168,7 @@ TEST(ModulesTest, BuiltinModules3) {
EXPECT_EQ(nullptr, deno_last_exception(d));

static deno_mod a =
deno_mod_new(d, "a.js",
deno_mod_new(d, true, "a.js",
"import * as b1 from 'builtin'\n"
"import * as b2 from 'b.js'\n"
"if (b1.foo != 'bar') throw Error('bad1');\n"
Expand All @@ -181,7 +181,7 @@ TEST(ModulesTest, BuiltinModules3) {
EXPECT_STREQ("builtin", deno_mod_imports_get(d, a, 0));
EXPECT_STREQ("b.js", deno_mod_imports_get(d, a, 1));

static deno_mod b = deno_mod_new(d, "b.js",
static deno_mod b = deno_mod_new(d, false, "b.js",
"import { foo } from 'builtin';\n"
"export function bar() { return foo }\n");
EXPECT_NE(b, 0);
Expand Down Expand Up @@ -218,7 +218,7 @@ TEST(ModulesTest, ResolutionError) {
Deno* d = deno_new(deno_config{0, empty, empty, recv_cb});
EXPECT_EQ(0, exec_count);

static deno_mod a = deno_mod_new(d, "a.js",
static deno_mod a = deno_mod_new(d, true, "a.js",
"import 'bad'\n"
"libdeno.send(new Uint8Array([4]));");
EXPECT_NE(a, 0);
Expand Down Expand Up @@ -252,7 +252,7 @@ TEST(ModulesTest, ImportMetaUrl) {
EXPECT_EQ(0, exec_count);

static deno_mod a =
deno_mod_new(d, "a.js",
deno_mod_new(d, true, "a.js",
"if ('a.js' != import.meta.url) throw 'hmm'\n"
"libdeno.send(new Uint8Array([4]));");
EXPECT_NE(a, 0);
Expand All @@ -266,3 +266,32 @@ TEST(ModulesTest, ImportMetaUrl) {
EXPECT_EQ(nullptr, deno_last_exception(d));
EXPECT_EQ(1, exec_count);
}

TEST(ModulesTest, ImportMetaMain) {
Deno* d = deno_new(deno_config{0, empty, empty, recv_cb});

const char* throw_not_main_src = "if (!import.meta.main) throw 'err'";
static deno_mod throw_not_main =
deno_mod_new(d, true, "a.js", throw_not_main_src);
EXPECT_NE(throw_not_main, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));

deno_mod_instantiate(d, d, throw_not_main, nullptr);
EXPECT_EQ(nullptr, deno_last_exception(d));

deno_mod_evaluate(d, d, throw_not_main);
EXPECT_EQ(nullptr, deno_last_exception(d));

const char* throw_main_src = "if (import.meta.main) throw 'err'";
static deno_mod throw_main = deno_mod_new(d, false, "b.js", throw_main_src);
EXPECT_NE(throw_main, 0);
EXPECT_EQ(nullptr, deno_last_exception(d));

deno_mod_instantiate(d, d, throw_main, nullptr);
EXPECT_EQ(nullptr, deno_last_exception(d));

deno_mod_evaluate(d, d, throw_main);
EXPECT_EQ(nullptr, deno_last_exception(d));

deno_delete(d);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test!

}
7 changes: 4 additions & 3 deletions src/isolate.rs
Expand Up @@ -283,6 +283,7 @@ impl Isolate {

pub fn mod_new(
&mut self,
main: bool,
name: String,
source: String,
) -> Result<libdeno::deno_mod, JSError> {
Expand All @@ -293,7 +294,7 @@ impl Isolate {
let source_ptr = source_.as_ptr() as *const i8;

let id = unsafe {
libdeno::deno_mod_new(self.libdeno_isolate, name_ptr, source_ptr)
libdeno::deno_mod_new(self.libdeno_isolate, main, name_ptr, source_ptr)
};
if let Some(js_error) = self.last_exception() {
assert_eq!(id, 0);
Expand Down Expand Up @@ -345,7 +346,7 @@ impl Isolate {
&referrer_name,
)?;
let child_id =
self.mod_new(out.module_name.clone(), out.js_source())?;
self.mod_new(false, out.module_name.clone(), out.js_source())?;

self.mod_load_deps(child_id)?;
}
Expand Down Expand Up @@ -391,7 +392,7 @@ impl Isolate {
.map_err(RustOrJsError::from)?;

let id = self
.mod_new(out.module_name.clone(), out.js_source())
.mod_new(true, out.module_name.clone(), out.js_source())
.map_err(RustOrJsError::from)?;

self.mod_load_deps(id)?;
Expand Down
1 change: 1 addition & 0 deletions src/libdeno.rs
Expand Up @@ -154,6 +154,7 @@ extern "C" {

pub fn deno_mod_new(
i: *const isolate,
main: bool,
name: *const c_char,
source: *const c_char,
) -> deno_mod;
Expand Down
2 changes: 1 addition & 1 deletion tests/import_meta.ts
@@ -1,3 +1,3 @@
console.log("import_meta", import.meta.url);
console.log("import_meta", import.meta.url, import.meta.main);

import "import_meta2.ts";
4 changes: 2 additions & 2 deletions tests/import_meta.ts.out
@@ -1,2 +1,2 @@
import_meta2 [WILDCARD]import_meta2.ts
import_meta [WILDCARD]import_meta.ts
import_meta2 [WILDCARD]import_meta2.ts false
import_meta [WILDCARD]import_meta.ts true
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion tests/import_meta2.ts
@@ -1 +1 @@
console.log("import_meta2", import.meta.url);
console.log("import_meta2", import.meta.url, import.meta.main);