mirror of
https://github.com/denoland/deno
synced 2024-08-25 02:57:22 +00:00
Remove 'deno' builtin module (#1895)
This commit is contained in:
parent
cd1992aeaa
commit
24d6bf6aeb
|
@ -39,9 +39,6 @@ import libEsnextBigintDts from "/third_party/node_modules/typescript/lib/lib.esn
|
|||
import libEsnextDts from "/third_party/node_modules/typescript/lib/lib.esnext.d.ts!string";
|
||||
import libEsnextIntlDts from "/third_party/node_modules/typescript/lib/lib.esnext.intl.d.ts!string";
|
||||
import libEsnextSymbolDts from "/third_party/node_modules/typescript/lib/lib.esnext.symbol.d.ts!string";
|
||||
|
||||
// Static definitions
|
||||
import typescriptDts from "/third_party/node_modules/typescript/lib/typescript.d.ts!string";
|
||||
// tslint:enable:max-line-length
|
||||
|
||||
// @internal
|
||||
|
@ -78,8 +75,5 @@ export const assetSourceCode: { [key: string]: string } = {
|
|||
"lib.esnext.asynciterable.d.ts": libEsnextAsynciterablesDts,
|
||||
"lib.esnext.bigint.d.ts": libEsnextBigintDts,
|
||||
"lib.esnext.intl.d.ts": libEsnextIntlDts,
|
||||
"lib.esnext.symbol.d.ts": libEsnextSymbolDts,
|
||||
|
||||
// Static definitions
|
||||
"typescript.d.ts": typescriptDts
|
||||
"lib.esnext.symbol.d.ts": libEsnextSymbolDts
|
||||
};
|
||||
|
|
|
@ -477,15 +477,7 @@ class Compiler implements ts.LanguageServiceHost, ts.FormatDiagnosticsHost {
|
|||
this._log("resolveModuleNames()", { moduleNames, containingFile });
|
||||
const resolvedModuleNames: ts.ResolvedModuleFull[] = [];
|
||||
for (const moduleName of moduleNames) {
|
||||
let moduleMetaData: ModuleMetaData;
|
||||
if (moduleName === "deno") {
|
||||
// builtin modules are part of the runtime lib
|
||||
moduleMetaData = this._getModuleMetaData(LIB_RUNTIME)!;
|
||||
} else if (moduleName === "typescript") {
|
||||
moduleMetaData = this._getModuleMetaData(`${ASSETS}/typescript.d.ts`)!;
|
||||
} else {
|
||||
moduleMetaData = this._resolveModule(moduleName, containingFile);
|
||||
}
|
||||
const moduleMetaData = this._resolveModule(moduleName, containingFile);
|
||||
// According to the interface we shouldn't return `undefined` but if we
|
||||
// fail to return the same length of modules to those we cannot resolve
|
||||
// then TypeScript fails on an assertion that the lengths can't be
|
||||
|
|
|
@ -24,9 +24,6 @@ interface Libdeno {
|
|||
|
||||
shared: ArrayBuffer;
|
||||
|
||||
// DEPRECATED
|
||||
builtinModules: { [s: string]: object };
|
||||
|
||||
/** Evaluate provided code in the current context.
|
||||
* It differs from eval(...) in that it does not create a new context.
|
||||
* Returns an array: [output, errInfo].
|
||||
|
|
|
@ -6,7 +6,6 @@ import "./globals";
|
|||
|
||||
import { assert, log } from "./util";
|
||||
import * as os from "./os";
|
||||
import { libdeno } from "./libdeno";
|
||||
import { args } from "./deno";
|
||||
import { replLoop } from "./repl";
|
||||
import { setVersions } from "./version";
|
||||
|
@ -22,10 +21,6 @@ import libDts from "gen/lib/lib.deno_runtime.d.ts!string";
|
|||
export default function denoMain() {
|
||||
const startResMsg = os.start();
|
||||
|
||||
// TODO(kitsonk) remove when import "deno" no longer supported
|
||||
libdeno.builtinModules["deno"] = deno;
|
||||
Object.freeze(libdeno.builtinModules);
|
||||
|
||||
setVersions(startResMsg.denoVersion()!, startResMsg.v8Version()!);
|
||||
|
||||
// handle `--version`
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||
import { testPerm, assert, assertEquals } from "./test_util.ts";
|
||||
import { Permission } from "deno";
|
||||
|
||||
const knownPermissions: Permission[] = ["run", "read", "write", "net", "env"];
|
||||
const knownPermissions: Deno.Permission[] = [
|
||||
"run",
|
||||
"read",
|
||||
"write",
|
||||
"net",
|
||||
"env"
|
||||
];
|
||||
|
||||
for (let grant of knownPermissions) {
|
||||
testPerm({ [grant]: true }, function envGranted() {
|
||||
|
|
|
@ -289,14 +289,6 @@ void Send(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|||
}
|
||||
}
|
||||
|
||||
v8::Local<v8::Object> DenoIsolate::GetBuiltinModules() {
|
||||
v8::EscapableHandleScope handle_scope(isolate_);
|
||||
if (builtin_modules_.IsEmpty()) {
|
||||
builtin_modules_.Reset(isolate_, v8::Object::New(isolate_));
|
||||
}
|
||||
return handle_scope.Escape(builtin_modules_.Get(isolate_));
|
||||
}
|
||||
|
||||
v8::ScriptOrigin ModuleOrigin(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> resource_name) {
|
||||
return v8::ScriptOrigin(resource_name, v8::Local<v8::Integer>(),
|
||||
|
@ -350,15 +342,6 @@ deno_mod DenoIsolate::RegisterModule(bool main, const char* name,
|
|||
return id;
|
||||
}
|
||||
|
||||
void BuiltinModules(v8::Local<v8::Name> property,
|
||||
const v8::PropertyCallbackInfo<v8::Value>& info) {
|
||||
v8::Isolate* isolate = info.GetIsolate();
|
||||
DenoIsolate* d = DenoIsolate::FromIsolate(isolate);
|
||||
DCHECK_EQ(d->isolate_, isolate);
|
||||
v8::Locker locker(d->isolate_);
|
||||
info.GetReturnValue().Set(d->GetBuiltinModules());
|
||||
}
|
||||
|
||||
void Shared(v8::Local<v8::Name> property,
|
||||
const v8::PropertyCallbackInfo<v8::Value>& info) {
|
||||
v8::Isolate* isolate = info.GetIsolate();
|
||||
|
@ -530,11 +513,6 @@ void InitializeContext(v8::Isolate* isolate, v8::Local<v8::Context> context) {
|
|||
|
||||
CHECK(deno_val->SetAccessor(context, deno::v8_str("shared"), Shared)
|
||||
.FromJust());
|
||||
|
||||
CHECK(
|
||||
deno_val
|
||||
->SetAccessor(context, deno::v8_str("builtinModules"), BuiltinModules)
|
||||
.FromJust());
|
||||
}
|
||||
|
||||
void MessageCallback(v8::Local<v8::Message> message,
|
||||
|
|
|
@ -67,7 +67,6 @@ class DenoIsolate {
|
|||
void AddIsolate(v8::Isolate* isolate);
|
||||
|
||||
deno_mod RegisterModule(bool main, const char* name, const char* source);
|
||||
v8::Local<v8::Object> GetBuiltinModules();
|
||||
void ClearModules();
|
||||
|
||||
ModuleInfo* GetModuleInfo(deno_mod id) {
|
||||
|
@ -109,7 +108,6 @@ class DenoIsolate {
|
|||
size_t next_zero_copy_id_;
|
||||
void* user_data_;
|
||||
|
||||
v8::Persistent<v8::Object> builtin_modules_;
|
||||
std::map<deno_mod, ModuleInfo> mods_;
|
||||
std::map<std::string, deno_mod> mods_by_name_;
|
||||
deno_resolve_cb resolve_cb_;
|
||||
|
@ -159,8 +157,6 @@ void EvalContext(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|||
void ErrorToJSON(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
void Shared(v8::Local<v8::Name> property,
|
||||
const v8::PropertyCallbackInfo<v8::Value>& info);
|
||||
void BuiltinModules(v8::Local<v8::Name> property,
|
||||
const v8::PropertyCallbackInfo<v8::Value>& info);
|
||||
void MessageCallback(v8::Local<v8::Message> message, v8::Local<v8::Value> data);
|
||||
static intptr_t external_references[] = {
|
||||
reinterpret_cast<intptr_t>(Print),
|
||||
|
@ -169,7 +165,6 @@ static intptr_t external_references[] = {
|
|||
reinterpret_cast<intptr_t>(EvalContext),
|
||||
reinterpret_cast<intptr_t>(ErrorToJSON),
|
||||
reinterpret_cast<intptr_t>(Shared),
|
||||
reinterpret_cast<intptr_t>(BuiltinModules),
|
||||
reinterpret_cast<intptr_t>(MessageCallback),
|
||||
0};
|
||||
|
||||
|
|
|
@ -19,42 +19,6 @@ using v8::ScriptOrigin;
|
|||
using v8::String;
|
||||
using v8::Value;
|
||||
|
||||
std::string BuiltinModuleSrc(Local<Context> context, Local<String> specifier) {
|
||||
auto* isolate = context->GetIsolate();
|
||||
DenoIsolate* d = DenoIsolate::FromIsolate(isolate);
|
||||
v8::Isolate::Scope isolate_scope(isolate);
|
||||
v8::EscapableHandleScope handle_scope(isolate);
|
||||
v8::Context::Scope context_scope(context);
|
||||
|
||||
v8::String::Utf8Value specifier_utf8val(isolate, specifier);
|
||||
const char* specifier_cstr = *specifier_utf8val;
|
||||
|
||||
auto builtin_modules = d->GetBuiltinModules();
|
||||
auto val = builtin_modules->Get(context, specifier).ToLocalChecked();
|
||||
CHECK(val->IsObject());
|
||||
auto obj = val->ToObject(isolate);
|
||||
|
||||
// In order to export obj as a module, we must iterate over its properties
|
||||
// and export them each individually.
|
||||
// TODO(ry) Find a better way to do this.
|
||||
std::string src = "let globalEval = eval\nlet g = globalEval('this');\n";
|
||||
auto names = obj->GetOwnPropertyNames(context).ToLocalChecked();
|
||||
for (uint32_t i = 0; i < names->Length(); i++) {
|
||||
auto name = names->Get(context, i).ToLocalChecked();
|
||||
v8::String::Utf8Value name_utf8val(isolate, name);
|
||||
const char* name_cstr = *name_utf8val;
|
||||
// TODO(ry) use format string.
|
||||
src.append("export const ");
|
||||
src.append(name_cstr);
|
||||
src.append(" = g.libdeno.builtinModules.");
|
||||
src.append(specifier_cstr);
|
||||
src.append(".");
|
||||
src.append(name_cstr);
|
||||
src.append(";\n");
|
||||
}
|
||||
return src;
|
||||
}
|
||||
|
||||
v8::MaybeLocal<v8::Module> ResolveCallback(Local<Context> context,
|
||||
Local<String> specifier,
|
||||
Local<Module> referrer) {
|
||||
|
@ -66,8 +30,6 @@ v8::MaybeLocal<v8::Module> ResolveCallback(Local<Context> context,
|
|||
|
||||
v8::EscapableHandleScope handle_scope(isolate);
|
||||
|
||||
auto builtin_modules = d->GetBuiltinModules();
|
||||
|
||||
deno_mod referrer_id = referrer->GetIdentityHash();
|
||||
auto* referrer_info = d->GetModuleInfo(referrer_id);
|
||||
CHECK_NOT_NULL(referrer_info);
|
||||
|
@ -79,21 +41,7 @@ v8::MaybeLocal<v8::Module> ResolveCallback(Local<Context> context,
|
|||
v8::String::Utf8Value req_utf8(isolate, req);
|
||||
std::string req_str(*req_utf8);
|
||||
|
||||
deno_mod id = 0;
|
||||
{
|
||||
bool has_builtin = builtin_modules->Has(context, specifier).ToChecked();
|
||||
if (has_builtin) {
|
||||
auto it = d->mods_by_name_.find(req_str.c_str());
|
||||
if (it != d->mods_by_name_.end()) {
|
||||
id = it->second;
|
||||
} else {
|
||||
std::string src = BuiltinModuleSrc(context, specifier);
|
||||
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);
|
||||
}
|
||||
}
|
||||
deno_mod id = d->resolve_cb_(d->user_data_, req_str.c_str(), referrer_id);
|
||||
|
||||
// Note: id might be zero, in which case GetModuleInfo will return
|
||||
// nullptr.
|
||||
|
|
|
@ -64,157 +64,6 @@ TEST(ModulesTest, Resolution) {
|
|||
deno_delete(d);
|
||||
}
|
||||
|
||||
TEST(ModulesTest, BuiltinModules) {
|
||||
exec_count = 0; // Reset
|
||||
Deno* d = deno_new(deno_config{0, empty, empty, recv_cb});
|
||||
EXPECT_EQ(0, exec_count);
|
||||
|
||||
deno_execute(d, d, "setup.js",
|
||||
"libdeno.builtinModules['deno'] = { foo: 'bar' };");
|
||||
EXPECT_EQ(nullptr, deno_last_exception(d));
|
||||
|
||||
static deno_mod a =
|
||||
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"
|
||||
"if (deno.foo != 'bar') throw Error('foo');\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, false, "b.js", b_src);
|
||||
EXPECT_NE(b, 0);
|
||||
EXPECT_EQ(nullptr, deno_last_exception(d));
|
||||
|
||||
EXPECT_EQ(0, exec_count);
|
||||
|
||||
EXPECT_EQ(2u, deno_mod_imports_len(d, a));
|
||||
EXPECT_EQ(0u, deno_mod_imports_len(d, b));
|
||||
|
||||
EXPECT_STREQ("b.js", deno_mod_imports_get(d, a, 0));
|
||||
EXPECT_STREQ("deno", deno_mod_imports_get(d, a, 1));
|
||||
EXPECT_EQ(nullptr, deno_mod_imports_get(d, a, 2));
|
||||
EXPECT_EQ(nullptr, deno_mod_imports_get(d, b, 0));
|
||||
|
||||
static int resolve_count = 0;
|
||||
auto resolve_cb = [](void* user_data, const char* specifier,
|
||||
deno_mod referrer) {
|
||||
EXPECT_EQ(referrer, a);
|
||||
EXPECT_STREQ(specifier, "b.js");
|
||||
resolve_count++;
|
||||
return b;
|
||||
};
|
||||
|
||||
deno_mod_instantiate(d, d, b, resolve_cb);
|
||||
EXPECT_EQ(nullptr, deno_last_exception(d));
|
||||
EXPECT_EQ(0, resolve_count);
|
||||
EXPECT_EQ(0, exec_count);
|
||||
|
||||
deno_mod_instantiate(d, d, a, resolve_cb);
|
||||
EXPECT_EQ(nullptr, deno_last_exception(d));
|
||||
EXPECT_EQ(1, resolve_count);
|
||||
EXPECT_EQ(0, exec_count);
|
||||
|
||||
deno_mod_evaluate(d, d, a);
|
||||
EXPECT_EQ(nullptr, deno_last_exception(d));
|
||||
EXPECT_EQ(1, resolve_count);
|
||||
EXPECT_EQ(1, exec_count);
|
||||
|
||||
deno_delete(d);
|
||||
}
|
||||
|
||||
TEST(ModulesTest, BuiltinModules2) {
|
||||
exec_count = 0; // Reset
|
||||
Deno* d = deno_new(deno_config{0, empty, empty, recv_cb});
|
||||
EXPECT_EQ(0, exec_count);
|
||||
|
||||
deno_execute(d, d, "setup.js",
|
||||
"libdeno.builtinModules['builtin1'] = { foo: 'bar' }; \n"
|
||||
"libdeno.builtinModules['builtin2'] = { hello: 'world' }; \n");
|
||||
EXPECT_EQ(nullptr, deno_last_exception(d));
|
||||
|
||||
static deno_mod a =
|
||||
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"
|
||||
"if (b2.hello != 'world') throw Error('bad2');\n"
|
||||
"libdeno.send(new Uint8Array([4]));");
|
||||
EXPECT_NE(a, 0);
|
||||
EXPECT_EQ(nullptr, deno_last_exception(d));
|
||||
|
||||
EXPECT_EQ(2u, deno_mod_imports_len(d, a));
|
||||
EXPECT_STREQ("builtin1", deno_mod_imports_get(d, a, 0));
|
||||
EXPECT_STREQ("builtin2", deno_mod_imports_get(d, a, 1));
|
||||
|
||||
deno_mod_instantiate(d, d, a, nullptr);
|
||||
EXPECT_EQ(nullptr, deno_last_exception(d));
|
||||
EXPECT_EQ(0, exec_count);
|
||||
|
||||
deno_mod_evaluate(d, d, a);
|
||||
EXPECT_EQ(nullptr, deno_last_exception(d));
|
||||
EXPECT_EQ(1, exec_count);
|
||||
|
||||
deno_delete(d);
|
||||
}
|
||||
|
||||
TEST(ModulesTest, BuiltinModules3) {
|
||||
exec_count = 0; // Reset
|
||||
Deno* d = deno_new(deno_config{0, empty, empty, recv_cb});
|
||||
EXPECT_EQ(0, exec_count);
|
||||
|
||||
deno_execute(d, d, "setup.js",
|
||||
"libdeno.builtinModules['builtin'] = { foo: 'bar' };");
|
||||
EXPECT_EQ(nullptr, deno_last_exception(d));
|
||||
|
||||
static deno_mod a =
|
||||
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"
|
||||
"if (b2.bar() != 'bar') throw Error('bad2');\n"
|
||||
"libdeno.send(new Uint8Array([4]));");
|
||||
EXPECT_NE(a, 0);
|
||||
EXPECT_EQ(nullptr, deno_last_exception(d));
|
||||
|
||||
EXPECT_EQ(2u, deno_mod_imports_len(d, a));
|
||||
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, false, "b.js",
|
||||
"import { foo } from 'builtin';\n"
|
||||
"export function bar() { return foo }\n");
|
||||
EXPECT_NE(b, 0);
|
||||
EXPECT_EQ(nullptr, deno_last_exception(d));
|
||||
|
||||
static int resolve_count = 0;
|
||||
auto resolve_cb = [](void* user_data, const char* specifier,
|
||||
deno_mod referrer) {
|
||||
EXPECT_EQ(referrer, a);
|
||||
EXPECT_STREQ(specifier, "b.js");
|
||||
resolve_count++;
|
||||
return b;
|
||||
};
|
||||
|
||||
deno_mod_instantiate(d, d, a, resolve_cb);
|
||||
EXPECT_EQ(nullptr, deno_last_exception(d));
|
||||
EXPECT_EQ(1, resolve_count);
|
||||
EXPECT_EQ(0, exec_count);
|
||||
|
||||
deno_mod_instantiate(d, d, b, resolve_cb);
|
||||
EXPECT_EQ(nullptr, deno_last_exception(d));
|
||||
EXPECT_EQ(1, resolve_count);
|
||||
EXPECT_EQ(0, exec_count);
|
||||
|
||||
deno_mod_evaluate(d, d, a);
|
||||
EXPECT_EQ(nullptr, deno_last_exception(d));
|
||||
EXPECT_EQ(1, exec_count);
|
||||
|
||||
deno_delete(d);
|
||||
}
|
||||
|
||||
TEST(ModulesTest, ResolutionError) {
|
||||
exec_count = 0; // Reset
|
||||
Deno* d = deno_new(deno_config{0, empty, empty, recv_cb});
|
||||
|
|
|
@ -342,12 +342,6 @@ impl Isolate {
|
|||
let specifier_c: &CStr = unsafe { CStr::from_ptr(specifier_ptr) };
|
||||
let specifier: &str = specifier_c.to_str().unwrap();
|
||||
|
||||
// TODO(ry) This shouldn't be necessary here. builtin modules should be
|
||||
// taken care of at the libdeno level.
|
||||
if specifier == "deno" {
|
||||
continue;
|
||||
}
|
||||
|
||||
let (name, _local_filename) = self
|
||||
.state
|
||||
.dir
|
||||
|
|
|
@ -1,2 +1 @@
|
|||
import { noColor } from "deno";
|
||||
console.log("noColor", noColor);
|
||||
console.log("noColor", Deno.noColor);
|
||||
|
|
|
@ -485,7 +485,6 @@ export function main({
|
|||
declarationProject,
|
||||
filePath: `${basePath}/js/deno.d.ts`,
|
||||
globalInterfaceName: "Window",
|
||||
moduleName: `"deno"`,
|
||||
namespaceName: "Deno",
|
||||
targetSourceFile: libDTs
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue