From c9614d86c190b98bd8f0df9e17272387c3bad1d5 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sat, 30 Mar 2019 14:45:36 -0400 Subject: Move //libdeno to //core/libdeno (#2015) Fixes some sed errors introduced in c43cfe. Unfortunately moving libdeno required splitting build.rs into two parts, one for cli and one for core. I've also removed the arm64 build - it's complicating things at this re-org and we're not even testing it. I need to swing back to it and get tools/test.py running for it. --- .appveyor.yml | 6 +- .travis.yml | 24 -- BUILD.gn | 277 +-------------- Cargo.toml | 53 +-- build.rs | 152 -------- cli/BUILD.gn | 270 ++++++++++++++ cli/Cargo.toml | 51 +++ cli/build.rs | 20 ++ cli/js_errors.rs | 20 +- cli/msg.fbs | 2 +- cli/msg.rs | 2 +- cli/startup_data.rs | 20 +- core/BUILD.gn | 27 +- core/build.rs | 30 ++ core/isolate.rs | 10 +- core/libdeno.rs | 0 core/libdeno/BUILD.gn | 108 ++++++ core/libdeno/api.cc | 231 ++++++++++++ core/libdeno/binding.cc | 563 ++++++++++++++++++++++++++++++ core/libdeno/deno.h | 112 ++++++ core/libdeno/exceptions.cc | 217 ++++++++++++ core/libdeno/exceptions.h | 23 ++ core/libdeno/file_util.cc | 90 +++++ core/libdeno/file_util.h | 14 + core/libdeno/file_util_test.cc | 46 +++ core/libdeno/internal.h | 199 +++++++++++ core/libdeno/libdeno.d.ts | 40 +++ core/libdeno/libdeno_test.cc | 329 +++++++++++++++++ core/libdeno/libdeno_test.js | 197 +++++++++++ core/libdeno/modules.cc | 154 ++++++++ core/libdeno/modules_test.cc | 148 ++++++++ core/libdeno/snapshot_creator.cc | 47 +++ core/libdeno/test.cc | 31 ++ core/libdeno/test.h | 11 + deno.gni | 65 ++++ gn.rs | 110 ++++++ js/assets.ts | 2 +- js/chmod.ts | 2 +- js/compiler.ts | 2 +- js/copy_file.ts | 2 +- js/dir.ts | 2 +- js/dispatch.ts | 2 +- js/errors.ts | 4 +- js/fetch.ts | 2 +- js/file_info.ts | 2 +- js/files.ts | 2 +- js/format_error.ts | 2 +- js/main.ts | 2 +- js/make_temp_dir.ts | 2 +- js/metrics.ts | 2 +- js/mkdir.ts | 2 +- js/net.ts | 2 +- js/os.ts | 2 +- js/performance.ts | 2 +- js/permissions.ts | 2 +- js/process.ts | 2 +- js/read_dir.ts | 2 +- js/read_link.ts | 2 +- js/remove.ts | 2 +- js/rename.ts | 2 +- js/repl.ts | 2 +- js/resources.ts | 2 +- js/stat.ts | 2 +- js/symlink.ts | 2 +- js/timers.ts | 2 +- js/truncate.ts | 2 +- js/workers.ts | 2 +- libdeno/BUILD.gn | 108 ------ libdeno/api.cc | 231 ------------ libdeno/binding.cc | 563 ------------------------------ libdeno/deno.gni | 64 ---- libdeno/deno.h | 112 ------ libdeno/exceptions.cc | 217 ------------ libdeno/exceptions.h | 23 -- libdeno/file_util.cc | 90 ----- libdeno/file_util.h | 14 - libdeno/file_util_test.cc | 46 --- libdeno/internal.h | 199 ----------- libdeno/libdeno.d.ts | 40 --- libdeno/libdeno_test.cc | 329 ----------------- libdeno/libdeno_test.js | 197 ----------- libdeno/modules.cc | 154 -------- libdeno/modules_test.cc | 148 -------- libdeno/snapshot_creator.cc | 47 --- libdeno/test.cc | 31 -- libdeno/test.h | 11 - rollup.config.js | 4 +- tools/benchmark.py | 12 +- tools/format.py | 9 +- tools/lint.py | 2 +- tools/ts_library_builder/build_library.ts | 2 +- tools/ts_library_builder/main.ts | 2 +- tsconfig.json | 2 +- 93 files changed, 3211 insertions(+), 3209 deletions(-) delete mode 100644 build.rs create mode 100644 cli/BUILD.gn create mode 100644 cli/Cargo.toml create mode 100644 cli/build.rs create mode 100644 core/build.rs mode change 100755 => 100644 core/libdeno.rs create mode 100644 core/libdeno/BUILD.gn create mode 100644 core/libdeno/api.cc create mode 100644 core/libdeno/binding.cc create mode 100644 core/libdeno/deno.h create mode 100644 core/libdeno/exceptions.cc create mode 100644 core/libdeno/exceptions.h create mode 100644 core/libdeno/file_util.cc create mode 100644 core/libdeno/file_util.h create mode 100644 core/libdeno/file_util_test.cc create mode 100644 core/libdeno/internal.h create mode 100644 core/libdeno/libdeno.d.ts create mode 100644 core/libdeno/libdeno_test.cc create mode 100644 core/libdeno/libdeno_test.js create mode 100644 core/libdeno/modules.cc create mode 100644 core/libdeno/modules_test.cc create mode 100644 core/libdeno/snapshot_creator.cc create mode 100644 core/libdeno/test.cc create mode 100644 core/libdeno/test.h create mode 100644 deno.gni create mode 100644 gn.rs delete mode 100644 libdeno/BUILD.gn delete mode 100644 libdeno/api.cc delete mode 100644 libdeno/binding.cc delete mode 100644 libdeno/deno.gni delete mode 100644 libdeno/deno.h delete mode 100644 libdeno/exceptions.cc delete mode 100644 libdeno/exceptions.h delete mode 100644 libdeno/file_util.cc delete mode 100644 libdeno/file_util.h delete mode 100644 libdeno/file_util_test.cc delete mode 100644 libdeno/internal.h delete mode 100644 libdeno/libdeno.d.ts delete mode 100644 libdeno/libdeno_test.cc delete mode 100644 libdeno/libdeno_test.js delete mode 100644 libdeno/modules.cc delete mode 100644 libdeno/modules_test.cc delete mode 100644 libdeno/snapshot_creator.cc delete mode 100644 libdeno/test.cc delete mode 100644 libdeno/test.h diff --git a/.appveyor.yml b/.appveyor.yml index 70d26f773..60f1490fd 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -212,15 +212,15 @@ after_test: } # Verify that javascript and typescript files which are bundled by rollup are - # listed explicitly in BUILD.gn. This is not an air-tight check. + # listed explicitly in cli\BUILD.gn. This is not an air-tight check. # TODO: make rollup or another bundler write a depfile. - ps: |- $ignore = "test_util.ts", "unit_tests.ts", "*_test.ts" Get-ChildItem "js" -File -Force -Name | where { $name = $_; -not ($ignore | where { $name -like $_ }) } | - where { -not (Select-String -Pattern $_ -Path BUILD.gn ` + where { -not (Select-String -Pattern $_ -Path cli\BUILD.gn ` -SimpleMatch -CaseSensitive) } | - foreach { throw "$_ should be listed in BUILD.gn but isn't." } + foreach { throw "$_ should be listed in cli\BUILD.gn but isn't." } # Verify that generated ninja files do not use absolute path names. # If they do, it makes ccache/sccache much less effective. diff --git a/.travis.yml b/.travis.yml index 2e5991d93..8f23f11ba 100644 --- a/.travis.yml +++ b/.travis.yml @@ -109,30 +109,6 @@ jobs: branch: master repo: denoland/deno skip-cleanup: true - - - name: "cargo release linux arm64" - os: linux - dist: xenial - script: - - ./tools/lint.py - - ./tools/test_format.py - - rustup target add aarch64-unknown-linux-gnu - - sudo apt update - - |- - sudo apt -yq --no-install-suggests --no-install-recommends install \ - g++-5-aarch64-linux-gnu gcc-5-aarch64-linux-gnu g++-5-multilib \ - libc6-arm64-cross - - build/linux/sysroot_scripts/install-sysroot.py --arch=arm64 - - export DENO_BUILD_ARGS="target_cpu=\"arm64\" v8_target_cpu=\"arm64\"" - - export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER="/usr/bin/aarch64-linux-gnu-gcc-5" - - export CC_aarch64_unknown_linux_gnu="/usr/bin/aarch64-linux-gnu-gcc-5" - - cargo build -vv --target=aarch64-unknown-linux-gnu --release --locked - - cargo build -vv --target=aarch64-unknown-linux-gnu --release --tests --locked - - sudo apt -yq install qemu qemu-user binfmt-support qemu-user-binfmt - - sudo ln -s /usr/aarch64-linux-gnu/lib/ld-linux-aarch64.so.1 /lib/ld-linux-aarch64.so.1 - - export QEMU_LD_PREFIX=/usr/aarch64-linux-gnu - #- $CARGO_TARGET_DIR/aarch64-unknown-linux-gnu/release/deno tests/002_hello.ts - # - DENO_BUILD_MODE=release ./tools/test.py $CARGO_TARGET_DIR/aarch64-unknown-linux-gnu/release TODO(afinch7): Get the tests working - name: "cargo release linux x86_64" os: linux diff --git a/BUILD.gn b/BUILD.gn index bde36a7f2..204d88372 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1,184 +1,13 @@ -# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import("//build/toolchain/cc_wrapper.gni") -import("//build_extra/flatbuffers/flatbuffer.gni") -import("//build_extra/flatbuffers/rust/rust_flatbuffer.gni") import("//build_extra/rust/rust.gni") -import("//third_party/v8/gni/v8.gni") -import("//third_party/v8/snapshot_toolchain.gni") -import("libdeno/deno.gni") group("default") { testonly = true deps = [ - ":deno", ":hyper_hello", - ":test_rs", + "cli:deno", + "cli:test_rs", "core:default", - "libdeno:test_cc", - ] -} - -main_extern = [ - "core:deno_core", - - "$rust_build:ansi_term", - "$rust_build:atty", - "$rust_build:dirs", - "$rust_build:flatbuffers", - "$rust_build:futures", - "$rust_build:getopts", - "$rust_build:http", - "$rust_build:hyper", - "$rust_build:hyper_rustls", - "$rust_build:lazy_static", - "$rust_build:libc", - "$rust_build:log", - "$rust_build:rand", - "$rust_build:regex", - "$rust_build:remove_dir_all", - "$rust_build:ring", - "$rust_build:rustyline", - "$rust_build:serde_json", - "$rust_build:source_map_mappings", - "$rust_build:tempfile", - "$rust_build:tokio", - "$rust_build:tokio_executor", - "$rust_build:tokio_fs", - "$rust_build:tokio_io", - "$rust_build:tokio_process", - "$rust_build:tokio_threadpool", - "$rust_build:url", -] -if (is_win) { - main_extern += [ "$rust_build:winapi" ] -} - -ts_sources = [ - "js/assets.ts", - "js/blob.ts", - "js/buffer.ts", - "js/build.ts", - "js/chmod.ts", - "js/console_table.ts", - "js/compiler.ts", - "js/console.ts", - "js/copy_file.ts", - "js/core.ts", - "js/custom_event.ts", - "js/deno.ts", - "js/dir.ts", - "js/dispatch.ts", - "js/dom_types.ts", - "js/errors.ts", - "js/event.ts", - "js/event_target.ts", - "js/fetch.ts", - "js/format_error.ts", - "js/dom_file.ts", - "js/file_info.ts", - "js/files.ts", - "js/flatbuffers.ts", - "js/form_data.ts", - "js/globals.ts", - "js/headers.ts", - "js/io.ts", - "js/lib.web_assembly.d.ts", - "js/location.ts", - "js/main.ts", - "js/make_temp_dir.ts", - "js/metrics.ts", - "js/mkdir.ts", - "js/mock_builtin.js", - "js/net.ts", - "js/os.ts", - "js/permissions.ts", - "js/plugins.d.ts", - "js/process.ts", - "js/read_dir.ts", - "js/read_file.ts", - "js/read_link.ts", - "js/remove.ts", - "js/rename.ts", - "js/repl.ts", - "js/resources.ts", - "js/stat.ts", - "js/symlink.ts", - "js/text_encoding.ts", - "js/timers.ts", - "js/truncate.ts", - "js/types.ts", - "js/url.ts", - "js/url_search_params.ts", - "js/util.ts", - "js/window.ts", - "js/workers.ts", - "js/write_file.ts", - "js/performance.ts", - "js/version.ts", - "tsconfig.json", - - # Listing package.json and yarn.lock as sources ensures the bundle is rebuilt - # when npm packages are added/removed or their contents changes. - "package.json", - "third_party/yarn.lock", -] - -# When Cargo is driving the build, GN/Ninja are used to produce these non-Rust -# targets. Cargo handles all Rust source files and the final linking step. -group("deno_deps") { - deps = [ - ":msg_rs", - ":snapshot_compiler", - ":snapshot_deno", - "libdeno:libdeno_static_lib", - ] -} - -# Optimized dependencies for cross compiled builds. -# This can be removed once we get snapshots into cross compiled builds. -group("deno_deps_cross") { - testonly = true - deps = [ - ":compiler_bundle", - ":main_bundle", - ":msg_rs", - "libdeno:libdeno_static_lib", - "libdeno:test_cc", - ] -} - -# Reads the cargo info from Cargo.toml -deno_cargo_info = exec_script("build_extra/rust/get_cargo_info.py", - [ rebase_path("Cargo.toml", root_build_dir) ], - "json") - -rust_executable("deno") { - source_root = "cli/main.rs" - extern = main_extern - deps = [ - ":deno_deps", - ] - - # Extract version from Cargo.toml - # TODO integrate this into rust.gni by allowing the rust_executable template - # to specify a cargo.toml from which it will extract a version. - crate_version = deno_cargo_info.version - inputs = [ - "Cargo.toml", - ] -} - -rust_test("test_rs") { - source_root = "cli/main.rs" - extern = main_extern - deps = [ - ":deno_deps", - ] - - # Extract version from Cargo.toml - crate_version = deno_cargo_info.version - inputs = [ - "Cargo.toml", + "core/libdeno:test_cc", ] } @@ -189,103 +18,3 @@ rust_executable("hyper_hello") { "$rust_build:ring", ] } - -# Generates the core TypeScript type library for deno that will be -# included in the runtime bundle -run_node("deno_runtime_declaration") { - out_dir = target_gen_dir - sources = ts_sources - outputs = [ - "$out_dir/lib/lib.deno_runtime.d.ts", - ] - deps = [ - ":msg_ts", - ] - inputs = ts_sources + [ - "tools/ts_library_builder/tsconfig.json", - "tools/ts_library_builder/main.ts", - "tools/ts_library_builder/build_library.ts", - "tools/ts_library_builder/ast_util.ts", - ] - args = [ - rebase_path("node_modules/ts-node/dist/bin.js", root_build_dir), - "--project", - rebase_path("tools/ts_library_builder/tsconfig.json"), - "--skip-ignore", - rebase_path("tools/ts_library_builder/main.ts", root_build_dir), - "--basePath", - rebase_path(".", root_build_dir), - "--inline", - rebase_path("js/lib.web_assembly.d.ts", root_build_dir), - "--buildPath", - rebase_path(root_build_dir, root_build_dir), - "--outFile", - rebase_path(outputs[0], root_build_dir), - "--silent", - ] - if (is_debug) { - args += [ "--debug" ] - } -} - -bundle("main_bundle") { - out_dir = "$target_gen_dir/bundle/" - out_name = "main" - deps = [ - ":deno_runtime_declaration", - ":msg_ts", - ":write_gn_args", - ] - data = [ - "$target_gen_dir/gn_args.txt", - ] -} - -bundle("compiler_bundle") { - out_dir = "$target_gen_dir/bundle/" - out_name = "compiler" - deps = [ - ":deno_runtime_declaration", - ":msg_ts", - ":write_gn_args", - ] - data = [ - "$target_gen_dir/gn_args.txt", - ] -} - -ts_flatbuffer("msg_ts") { - sources = [ - "cli/msg.fbs", - ] -} - -rust_flatbuffer("msg_rs") { - sources = [ - "cli/msg.fbs", - ] -} - -# Generates $target_gen_dir/snapshot_deno.bin -snapshot("snapshot_deno") { - source_root = "$target_gen_dir/bundle/main.js" - deps = [ - ":main_bundle", - ] -} - -# Generates $target_gen_dir/snapshot_compiler.bin -snapshot("snapshot_compiler") { - source_root = "$target_gen_dir/bundle/compiler.js" - deps = [ - ":compiler_bundle", - ] -} - -action("write_gn_args") { - script = "//tools/write_gn_args.py" - outputs = [ - "$target_gen_dir/gn_args.txt", - ] - args = [ rebase_path(outputs[0], root_build_dir) ] -} diff --git a/Cargo.toml b/Cargo.toml index 7a0046bbc..cbd245a50 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,56 +1,5 @@ -# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -# Dummy package info required by `cargo fetch`. -# Use tools/sync_third_party.py to install deps after editing this file. -# Deno does not build with cargo. Deno uses a build system called gn. -# See build_extra/rust/BUILD.gn for the manually built configuration of rust -# crates. - [workspace] members = [ - "./", + "cli", "core", ] - -[[bin]] -name = "deno" -path = "cli/main.rs" - -[package] -name = "deno" -version = "0.3.5" -edition = "2018" - -[dependencies] -deno_core = { path = "./core" } - -ansi_term = "0.11.0" -atty = "0.2.11" -dirs = "1.0.5" -flatbuffers = "0.5.0" -futures = "0.1.25" -getopts = "0.2.18" -http = "0.1.16" -hyper = "0.12.25" -hyper-rustls = "0.16.1" -integer-atomics = "1.0.2" -lazy_static = "1.3.0" -libc = "0.2.51" -log = "0.4.6" -rand = "0.6.5" -regex = "1.1.2" -remove_dir_all = "0.5.1" -ring = "0.14.6" -rustyline = "3.0.0" -serde_json = "1.0.39" -source-map-mappings = "0.5.0" -tempfile = "3.0.7" -tokio = "0.1.18" -tokio-executor = "0.1.7" -tokio-fs = "0.1.6" -tokio-io = "0.1.12" -tokio-process = "0.2.3" -tokio-threadpool = "0.1.13" -url = "1.7.2" - -[target.'cfg(windows)'.dependencies] -winapi = "0.3.6" diff --git a/build.rs b/build.rs deleted file mode 100644 index 8dbbf0955..000000000 --- a/build.rs +++ /dev/null @@ -1,152 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. - -// Run "cargo build -vv" if you want to see gn output. - -#![deny(warnings)] - -use std::env; -use std::path::{self, Path, PathBuf}; -use std::process::Command; - -fn main() { - let gn_mode = if cfg!(target_os = "windows") { - // On Windows, we need to link with a release build of libdeno, because - // rust always uses the release CRT. - // TODO(piscisaureus): make linking with debug libdeno possible. - String::from("release") - } else { - // Cargo sets PROFILE to either "debug" or "release", which conveniently - // matches the build modes we support. - env::var("PROFILE").unwrap() - }; - - // Equivalent to target arch != host arch - let is_different_target_arch = - env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() != env::var("HOST") - .unwrap() - .as_str() - .split("-") - .collect::>()[0]; - - // If we are using the same target as the host's default - // "rustup target list" should show your default target - let is_default_target = - env::var("TARGET").unwrap() == env::var("HOST").unwrap(); - - let cwd = env::current_dir().unwrap(); - // If not using host default target the output folder will change - // target/release will become target/$TARGET/release - // Gn should also be using this output directory as well - // most things will work with gn using the default - // output directory but some tests depend on artifacts - // being in a specific directory relative to the main build output - let gn_out_path = cwd.join(format!( - "target/{}", - match is_default_target { - true => gn_mode.clone(), - false => format!("{}/{}", env::var("TARGET").unwrap(), gn_mode.clone()), - } - )); - let gn_out_dir = normalize_path(&gn_out_path); - - // Tell Cargo when to re-run this file. We do this first, so these directives - // can take effect even if something goes wrong later in the build process. - println!("cargo:rerun-if-env-changed=DENO_BUILD_PATH"); - // TODO: this is obviously not appropriate here. - println!("cargo:rerun-if-env-changed=APPVEYOR_REPO_COMMIT"); - - // Detect if we're being invoked by the rust language server (RLS). - // Unfortunately we can't detect whether we're being run by `cargo check`. - let check_only = env::var_os("CARGO") - .map(PathBuf::from) - .as_ref() - .and_then(|p| p.file_stem()) - .and_then(|f| f.to_str()) - .map(|s| s.starts_with("rls")) - .unwrap_or(false); - - // This helps Rust source files locate the snapshot, source map etc. - println!("cargo:rustc-env=GN_OUT_DIR={}", gn_out_dir); - - let gn_target; - - if check_only { - // When RLS is running "cargo check" to analyze the source code, we're not - // trying to build a working executable, rather we're just compiling all - // rust code. Therefore, make ninja build only 'msg_generated.rs'. - gn_target = "msg_rs"; - - // Enable the 'check_only' feature, which enables some workarounds in the - // rust source code to compile successfully without a bundle and snapshot - println!("cargo:rustc-cfg=feature=\"check-only\""); - } else { - // "Full" (non-RLS) build. - if is_different_target_arch { - gn_target = "deno_deps_cross"; - } else { - gn_target = "deno_deps"; - } - // Link with libdeno.a/.lib, which includes V8. - println!("cargo:rustc-link-search=native={}/obj/libdeno", gn_out_dir); - if cfg!(target_os = "windows") { - println!("cargo:rustc-link-lib=static=libdeno"); - } else { - println!("cargo:rustc-link-lib=static=deno"); - } - - // Link the system libraries that libdeno and V8 depend on. - if cfg!(any(target_os = "macos", target_os = "freebsd")) { - println!("cargo:rustc-link-lib=dylib=c++"); - } else if cfg!(target_os = "windows") { - for lib in vec!["dbghelp", "shlwapi", "winmm", "ws2_32"] { - println!("cargo:rustc-link-lib={}", lib); - } - } - } - - // If target_arch != host_arch disable snapshots. - // v8 snapshots seem to not be compatible with binaries - // other than the ones used to gernerate them, - // so for non native architecture builds we don't - // have an easy way to generate these snapshots. - // We can't run any binary capable of generating - // compatible snapshots without emulating the - // target architecture. - if is_different_target_arch { - // no-snapshot-init is not related to v8_use_snapshots - println!("cargo:rustc-cfg=feature=\"no-snapshot-init\""); - } - - if !gn_out_path.join("build.ninja").exists() { - let status = Command::new("python") - .env("DENO_BUILD_PATH", &gn_out_dir) - .env("DENO_BUILD_MODE", &gn_mode) - .arg("./tools/setup.py") - .status() - .expect("setup.py failed"); - assert!(status.success()); - } - - let status = Command::new("python") - .env("DENO_BUILD_PATH", &gn_out_dir) - .env("DENO_BUILD_MODE", &gn_mode) - .arg("./tools/build.py") - .arg(gn_target) - .arg("-v") - .status() - .expect("build.py failed"); - assert!(status.success()); -} - -// Utility function to make a path absolute, normalizing it to use forward -// slashes only. The returned value is an owned String, otherwise panics. -fn normalize_path>(path: T) -> String { - path - .as_ref() - .to_str() - .unwrap() - .to_owned() - .chars() - .map(|c| if path::is_separator(c) { '/' } else { c }) - .collect() -} diff --git a/cli/BUILD.gn b/cli/BUILD.gn new file mode 100644 index 000000000..f6000a08f --- /dev/null +++ b/cli/BUILD.gn @@ -0,0 +1,270 @@ +# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import("//build/toolchain/cc_wrapper.gni") +import("//build_extra/flatbuffers/flatbuffer.gni") +import("//build_extra/flatbuffers/rust/rust_flatbuffer.gni") +import("//build_extra/rust/rust.gni") +import("//third_party/v8/gni/v8.gni") +import("//third_party/v8/snapshot_toolchain.gni") +import("../deno.gni") + +main_extern = [ + "../core:deno_core", + + "$rust_build:ansi_term", + "$rust_build:atty", + "$rust_build:dirs", + "$rust_build:flatbuffers", + "$rust_build:futures", + "$rust_build:getopts", + "$rust_build:http", + "$rust_build:hyper", + "$rust_build:hyper_rustls", + "$rust_build:lazy_static", + "$rust_build:libc", + "$rust_build:log", + "$rust_build:rand", + "$rust_build:regex", + "$rust_build:remove_dir_all", + "$rust_build:ring", + "$rust_build:rustyline", + "$rust_build:serde_json", + "$rust_build:source_map_mappings", + "$rust_build:tempfile", + "$rust_build:tokio", + "$rust_build:tokio_executor", + "$rust_build:tokio_fs", + "$rust_build:tokio_io", + "$rust_build:tokio_process", + "$rust_build:tokio_threadpool", + "$rust_build:url", +] +if (is_win) { + main_extern += [ "$rust_build:winapi" ] +} + +ts_sources = [ + "../js/assets.ts", + "../js/blob.ts", + "../js/buffer.ts", + "../js/build.ts", + "../js/chmod.ts", + "../js/console_table.ts", + "../js/compiler.ts", + "../js/console.ts", + "../js/copy_file.ts", + "../js/core.ts", + "../js/custom_event.ts", + "../js/deno.ts", + "../js/dir.ts", + "../js/dispatch.ts", + "../js/dom_types.ts", + "../js/errors.ts", + "../js/event.ts", + "../js/event_target.ts", + "../js/fetch.ts", + "../js/format_error.ts", + "../js/dom_file.ts", + "../js/file_info.ts", + "../js/files.ts", + "../js/flatbuffers.ts", + "../js/form_data.ts", + "../js/globals.ts", + "../js/headers.ts", + "../js/io.ts", + "../js/lib.web_assembly.d.ts", + "../js/location.ts", + "../js/main.ts", + "../js/make_temp_dir.ts", + "../js/metrics.ts", + "../js/mkdir.ts", + "../js/mock_builtin.js", + "../js/net.ts", + "../js/os.ts", + "../js/permissions.ts", + "../js/plugins.d.ts", + "../js/process.ts", + "../js/read_dir.ts", + "../js/read_file.ts", + "../js/read_link.ts", + "../js/remove.ts", + "../js/rename.ts", + "../js/repl.ts", + "../js/resources.ts", + "../js/stat.ts", + "../js/symlink.ts", + "../js/text_encoding.ts", + "../js/timers.ts", + "../js/truncate.ts", + "../js/types.ts", + "../js/url.ts", + "../js/url_search_params.ts", + "../js/util.ts", + "../js/window.ts", + "../js/workers.ts", + "../js/write_file.ts", + "../js/performance.ts", + "../js/version.ts", + "../tsconfig.json", + + # Listing package.json and yarn.lock as sources ensures the bundle is rebuilt + # when npm packages are added/removed or their contents changes. + "../package.json", + "../third_party/yarn.lock", +] + +# When Cargo is driving the build, GN/Ninja are used to produce these non-Rust +# targets. Cargo handles all Rust source files and the final linking step. +group("deno_deps") { + deps = [ + ":msg_rs", + ":snapshot_compiler", + ":snapshot_deno", + ] +} + +# Optimized dependencies for cross compiled builds. +# This can be removed once we get snapshots into cross compiled builds. +group("deno_deps_cross") { + testonly = true + deps = [ + ":compiler_bundle", + ":main_bundle", + ":msg_rs", + ] +} + +# Reads the cargo info from Cargo.toml +deno_cargo_info = exec_script( + "../build_extra/rust/get_cargo_info.py", + [ rebase_path("Cargo.toml", root_build_dir) ], "json") + +rust_executable("deno") { + source_root = "main.rs" + extern = main_extern + deps = [ + ":deno_deps", + ] + + # Extract version from Cargo.toml + # TODO integrate this into rust.gni by allowing the rust_executable template + # to specify a cargo.toml from which it will extract a version. + crate_version = deno_cargo_info.version + inputs = [ + "Cargo.toml", + ] +} + +rust_test("test_rs") { + source_root = "main.rs" + extern = main_extern + deps = [ + ":deno_deps", + ] + + # Extract version from Cargo.toml + crate_version = deno_cargo_info.version + inputs = [ + "Cargo.toml", + ] +} + + +# Generates the core TypeScript type library for deno that will be +# included in the runtime bundle +run_node("deno_runtime_declaration") { + out_dir = target_gen_dir + sources = ts_sources + outputs = [ + "$out_dir/lib/lib.deno_runtime.d.ts", + ] + deps = [ + ":msg_ts", + ] + inputs = ts_sources + [ + "//tools/ts_library_builder/tsconfig.json", + "//tools/ts_library_builder/main.ts", + "//tools/ts_library_builder/build_library.ts", + "//tools/ts_library_builder/ast_util.ts", + ] + args = [ + rebase_path("//node_modules/ts-node/dist/bin.js", root_build_dir), + "--project", + rebase_path("//tools/ts_library_builder/tsconfig.json"), + "--skip-ignore", + rebase_path("//tools/ts_library_builder/main.ts", root_build_dir), + "--basePath", + rebase_path("//", root_build_dir), + "--inline", + rebase_path("//js/lib.web_assembly.d.ts", root_build_dir), + "--buildPath", + rebase_path(root_build_dir, root_build_dir), + "--outFile", + rebase_path(outputs[0], root_build_dir), + "--silent", + ] + if (is_debug) { + args += [ "--debug" ] + } +} + +bundle("main_bundle") { + out_dir = "$target_gen_dir/bundle/" + out_name = "main" + deps = [ + ":deno_runtime_declaration", + ":msg_ts", + ":write_gn_args", + ] + data = [ + "$target_gen_dir/gn_args.txt", + ] +} + +bundle("compiler_bundle") { + out_dir = "$target_gen_dir/bundle/" + out_name = "compiler" + deps = [ + ":deno_runtime_declaration", + ":msg_ts", + ":write_gn_args", + ] + data = [ + "$target_gen_dir/gn_args.txt", + ] +} + +ts_flatbuffer("msg_ts") { + sources = [ + "msg.fbs", + ] +} + +rust_flatbuffer("msg_rs") { + sources = [ + "msg.fbs", + ] +} + +# Generates $target_gen_dir/snapshot_deno.bin +snapshot("snapshot_deno") { + source_root = "$target_gen_dir/bundle/main.js" + deps = [ + ":main_bundle", + ] +} + +# Generates $target_gen_dir/snapshot_compiler.bin +snapshot("snapshot_compiler") { + source_root = "$target_gen_dir/bundle/compiler.js" + deps = [ + ":compiler_bundle", + ] +} + +action("write_gn_args") { + script = "//tools/write_gn_args.py" + outputs = [ + "$target_gen_dir/gn_args.txt", + ] + args = [ rebase_path(outputs[0], root_build_dir) ] +} diff --git a/cli/Cargo.toml b/cli/Cargo.toml new file mode 100644 index 000000000..c58558887 --- /dev/null +++ b/cli/Cargo.toml @@ -0,0 +1,51 @@ +# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +# Dummy package info required by `cargo fetch`. +# Use tools/sync_third_party.py to install deps after editing this file. +# Deno does not build with cargo. Deno uses a build system called gn. +# See build_extra/rust/BUILD.gn for the manually built configuration of rust +# crates. + + +[[bin]] +name = "deno" +path = "main.rs" + +[package] +name = "deno" +version = "0.3.5" +edition = "2018" + +[dependencies] +deno_core = { path = "../core" } + +ansi_term = "0.11.0" +atty = "0.2.11" +dirs = "1.0.5" +flatbuffers = "0.5.0" +futures = "0.1.25" +getopts = "0.2.18" +http = "0.1.16" +hyper = "0.12.25" +hyper-rustls = "0.16.1" +integer-atomics = "1.0.2" +lazy_static = "1.3.0" +libc = "0.2.51" +log = "0.4.6" +rand = "0.6.5" +regex = "1.1.2" +remove_dir_all = "0.5.1" +ring = "0.14.6" +rustyline = "3.0.0" +serde_json = "1.0.39" +source-map-mappings = "0.5.0" +tempfile = "3.0.7" +tokio = "0.1.18" +tokio-executor = "0.1.7" +tokio-fs = "0.1.6" +tokio-io = "0.1.12" +tokio-process = "0.2.3" +tokio-threadpool = "0.1.13" +url = "1.7.2" + +[target.'cfg(windows)'.dependencies] +winapi = "0.3.6" diff --git a/cli/build.rs b/cli/build.rs new file mode 100644 index 000000000..4d50d8a5f --- /dev/null +++ b/cli/build.rs @@ -0,0 +1,20 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +// Run "cargo build -vv" if you want to see gn output. +mod gn { + include!("../gn.rs"); +} + +fn main() { + let build = gn::Build::setup(); + + let gn_target = if build.check_only { + // When RLS is running "cargo check" to analyze the source code, we're not + // trying to build a working executable, rather we're just compiling all + // rust code. Therefore, make ninja build only 'msg_generated.rs'. + "cli:msg_rs" + } else { + "cli:deno_deps" + }; + + build.run(gn_target); +} diff --git a/cli/js_errors.rs b/cli/js_errors.rs index 1cb9cb3a1..b478849d2 100644 --- a/cli/js_errors.rs +++ b/cli/js_errors.rs @@ -64,9 +64,9 @@ impl<'a> fmt::Display for JSErrorColor<'a> { let e = self.0; if e.script_resource_name.is_some() { let script_resource_name = e.script_resource_name.as_ref().unwrap(); - // Avoid showing internal code from gen/bundle/main.js - if script_resource_name != "gen/bundle/main.js" - && script_resource_name != "gen/bundle/compiler.js" + // Avoid showing internal code from gen/cli/bundle/main.js + if script_resource_name != "gen/cli/bundle/main.js" + && script_resource_name != "gen/cli/bundle/compiler.js" { if e.line_number.is_some() && e.start_column.is_some() { assert!(e.line_number.is_some()); @@ -216,14 +216,16 @@ fn builtin_source_map(_: &str) -> Option> { #[cfg(not(feature = "check-only"))] fn builtin_source_map(script_name: &str) -> Option> { match script_name { - "gen/bundle/main.js" => Some( - include_bytes!(concat!(env!("GN_OUT_DIR"), "/gen/bundle/main.js.map")) - .to_vec(), + "gen/cli/bundle/main.js" => Some( + include_bytes!(concat!( + env!("GN_OUT_DIR"), + "/gen/cli/bundle/main.js.map" + )).to_vec(), ), - "gen/bundle/compiler.js" => Some( + "gen/cli/bundle/compiler.js" => Some( include_bytes!(concat!( env!("GN_OUT_DIR"), - "/gen/bundle/compiler.js.map" + "/gen/cli/bundle/compiler.js.map" )).to_vec(), ), _ => None, @@ -381,7 +383,7 @@ mod tests { frames: vec![StackFrame { line: 11, column: 12, - script_name: "gen/bundle/main.js".to_string(), + script_name: "gen/cli/bundle/main.js".to_string(), function_name: "setLogDebug".to_string(), is_eval: false, is_constructor: false, diff --git a/cli/msg.fbs b/cli/msg.fbs index e6b860908..45f940f7d 100644 --- a/cli/msg.fbs +++ b/cli/msg.fbs @@ -388,7 +388,7 @@ table OpenRes { table Read { rid: uint32; - // (ptr, len) is passed as second parameter to libdeno.send(). + // (ptr, len) is passed as second parameter to Deno.core.send(). } table ReadRes { diff --git a/cli/msg.rs b/cli/msg.rs index 080f39de8..d4f62604c 100644 --- a/cli/msg.rs +++ b/cli/msg.rs @@ -11,7 +11,7 @@ use std::sync::atomic::Ordering; // GN_OUT_DIR is set either by build.rs (for the Cargo build), or by // build_extra/rust/run.py (for the GN+Ninja build). -include!(concat!(env!("GN_OUT_DIR"), "/gen/msg_generated.rs")); +include!(concat!(env!("GN_OUT_DIR"), "/gen/cli/msg_generated.rs")); impl<'a> From<&'a isolate_state::Metrics> for MetricsResArgs { fn from(m: &'a isolate_state::Metrics) -> Self { diff --git a/cli/startup_data.rs b/cli/startup_data.rs index 6e636e579..747e8b11d 100644 --- a/cli/startup_data.rs +++ b/cli/startup_data.rs @@ -7,19 +7,19 @@ pub fn deno_isolate_init() -> StartupData { debug!("Deno isolate init without snapshots."); #[cfg(not(feature = "check-only"))] let source_bytes = - include_bytes!(concat!(env!("GN_OUT_DIR"), "/gen/bundle/main.js")); + include_bytes!(concat!(env!("GN_OUT_DIR"), "/gen/cli/bundle/main.js")); #[cfg(feature = "check-only")] let source_bytes = vec![]; StartupData::Script(Script { - filename: "gen/bundle/main.js".to_string(), + filename: "gen/cli/bundle/main.js".to_string(), source: std::str::from_utf8(&source_bytes[..]).unwrap().to_string(), }) } else { debug!("Deno isolate init with snapshots."); #[cfg(not(any(feature = "check-only", feature = "no-snapshot-init")))] let data = - include_bytes!(concat!(env!("GN_OUT_DIR"), "/gen/snapshot_deno.bin")); + include_bytes!(concat!(env!("GN_OUT_DIR"), "/gen/cli/snapshot_deno.bin")); #[cfg(any(feature = "check-only", feature = "no-snapshot-init"))] let data = vec![]; @@ -33,20 +33,24 @@ pub fn compiler_isolate_init() -> StartupData { if cfg!(feature = "no-snapshot-init") { debug!("Deno isolate init without snapshots."); #[cfg(not(feature = "check-only"))] - let source_bytes = - include_bytes!(concat!(env!("GN_OUT_DIR"), "/gen/bundle/compiler.js")); + let source_bytes = include_bytes!(concat!( + env!("GN_OUT_DIR"), + "/gen/cli/bundle/compiler.js" + )); #[cfg(feature = "check-only")] let source_bytes = vec![]; StartupData::Script(Script { - filename: "gen/bundle/compiler.js".to_string(), + filename: "gen/cli/bundle/compiler.js".to_string(), source: std::str::from_utf8(&source_bytes[..]).unwrap().to_string(), }) } else { debug!("Deno isolate init with snapshots."); #[cfg(not(any(feature = "check-only", feature = "no-snapshot-init")))] - let data = - include_bytes!(concat!(env!("GN_OUT_DIR"), "/gen/snapshot_compiler.bin")); + let data = include_bytes!(concat!( + env!("GN_OUT_DIR"), + "/gen/cli/snapshot_compiler.bin" + )); #[cfg(any(feature = "check-only", feature = "no-snapshot-init"))] let data = vec![]; diff --git a/core/BUILD.gn b/core/BUILD.gn index c14820637..3439fa25d 100644 --- a/core/BUILD.gn +++ b/core/BUILD.gn @@ -3,16 +3,19 @@ import("//build_extra/rust/rust.gni") group("default") { testonly = true deps = [ + ":deno_core", ":deno_core_http_bench", ":deno_core_http_bench_test", ":deno_core_test", ] } -deno_core_deps = [ - "../libdeno:libdeno_static_lib", - "../libdeno:v8", -] +group("deno_core_deps") { + deps = [ + "libdeno:libdeno_static_lib", + "libdeno:v8", + ] +} # deno_core does not depend on flatbuffers nor tokio. main_extern = [ @@ -24,13 +27,17 @@ main_extern = [ rust_crate("deno_core") { source_root = "lib.rs" - deps = deno_core_deps + deps = [ + ":deno_core_deps", + ] extern = main_extern } rust_test("deno_core_test") { source_root = "lib.rs" - deps = deno_core_deps + deps = [ + ":deno_core_deps", + ] extern = main_extern } @@ -48,12 +55,16 @@ if (is_win) { rust_executable("deno_core_http_bench") { source_root = "http_bench.rs" - deps = deno_core_deps + deps = [ + ":deno_core_deps", + ] extern = http_bench_extern } rust_test("deno_core_http_bench_test") { source_root = "http_bench.rs" - deps = deno_core_deps + deps = [ + ":deno_core_deps", + ] extern = http_bench_extern } diff --git a/core/build.rs b/core/build.rs new file mode 100644 index 000000000..0cb775e5c --- /dev/null +++ b/core/build.rs @@ -0,0 +1,30 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +// Run "cargo build -vv" if you want to see gn output. +mod gn { + include!("../gn.rs"); +} + +fn main() { + let build = gn::Build::setup(); + + println!( + "cargo:rustc-link-search=native={}/obj/core/libdeno", + build.gn_out_dir + ); + if cfg!(target_os = "windows") { + println!("cargo:rustc-link-lib=static=libdeno"); + } else { + println!("cargo:rustc-link-lib=static=deno"); + } + + // Link the system libraries that libdeno and V8 depend on. + if cfg!(any(target_os = "macos", target_os = "freebsd")) { + println!("cargo:rustc-link-lib=dylib=c++"); + } else if cfg!(target_os = "windows") { + for lib in vec!["dbghelp", "shlwapi", "winmm", "ws2_32"] { + println!("cargo:rustc-link-lib={}", lib); + } + } + + build.run("core:deno_core_deps"); +} diff --git a/core/isolate.rs b/core/isolate.rs index f8ada23d4..a8ee22d47 100644 --- a/core/isolate.rs +++ b/core/isolate.rs @@ -125,7 +125,7 @@ impl Isolate { None => libdeno::deno_buf::empty(), }, shared: shared.as_deno_buf(), - recv_cb: Self::predispatch, + recv_cb: Self::pre_dispatch, }; let libdeno_isolate = unsafe { libdeno::deno_new(config) }; @@ -166,7 +166,7 @@ impl Isolate { } } - extern "C" fn predispatch( + extern "C" fn pre_dispatch( user_data: *mut c_void, control_argv0: deno_buf, zero_copy_buf: deno_buf, @@ -612,7 +612,7 @@ mod tests { } #[test] - fn testdispatch() { + fn test_dispatch() { let mut isolate = TestBehavior::setup(TestBehaviorMode::AsyncImmediate); js_check(isolate.execute( "filename.js", @@ -917,12 +917,12 @@ mod tests { } #[test] - fn overflow_res_multipledispatch_async() { + fn overflow_res_multiple_dispatch_async() { // TODO(ry) This test is quite slow due to memcpy-ing 100MB into JS. We // should optimize this. let mut isolate = TestBehavior::setup(TestBehaviorMode::OverflowResAsync); js_check(isolate.execute( - "overflow_res_multipledispatch_async.js", + "overflow_res_multiple_dispatch_async.js", r#" let asyncRecv = 0; Deno.core.setAsyncHandler((buf) => { diff --git a/core/libdeno.rs b/core/libdeno.rs old mode 100755 new mode 100644 diff --git a/core/libdeno/BUILD.gn b/core/libdeno/BUILD.gn new file mode 100644 index 000000000..eeec14eb5 --- /dev/null +++ b/core/libdeno/BUILD.gn @@ -0,0 +1,108 @@ +# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import("//deno.gni") +import("//third_party/v8/gni/v8.gni") + +config("deno_config") { + include_dirs = [ "//third_party/v8" ] # This allows us to v8/src/base/ libraries. + configs = [ "//third_party/v8:external_config" ] + cflags = [] + + if (is_debug) { + defines = [ "DEBUG" ] + } + + if (is_clang) { + cflags += [ + "-fcolor-diagnostics", + "-fansi-escape-codes", + ] + if (is_debug) { + cflags += [ "-glldb" ] + } + } + + if (is_win) { + # The `/Zl` ("omit default library name") flag makes the compiler produce + # object files that can link with both the static and dynamic CRT. + cflags += [ "/Zl" ] + } +} + +v8_source_set("v8") { + deps = [ + "//third_party/v8:v8", + "//third_party/v8:v8_libbase", + "//third_party/v8:v8_libplatform", + "//third_party/v8:v8_libsampler", + ] + configs = [ ":deno_config" ] +} + +# Only functionality needed for libdeno_test and snapshot_creator +# In particular no flatbuffers, no assets, no rust, no msg handlers. +# Because snapshots are slow, it's important that snapshot_creator's +# dependencies are minimal. +v8_source_set("libdeno") { + sources = [ + "api.cc", + "binding.cc", + "deno.h", + "exceptions.cc", + "exceptions.h", + "file_util.cc", + "file_util.h", + "internal.h", + "modules.cc", + ] + deps = [ + ":v8", + ] + configs = [ ":deno_config" ] +} + +# The cargo-driven build links with libdeno to pull in all non-rust code. +v8_static_library("libdeno_static_lib") { + output_name = "libdeno" + deps = [ + ":libdeno", + "//build/config:shared_library_deps", + ] + configs = [ ":deno_config" ] +} + +v8_executable("snapshot_creator") { + sources = [ + "snapshot_creator.cc", + ] + deps = [ + ":libdeno", + ] + configs = [ ":deno_config" ] +} + +v8_executable("test_cc") { + testonly = true + sources = [ + "file_util_test.cc", + "libdeno_test.cc", + "modules_test.cc", + "test.cc", + ] + deps = [ + ":libdeno", + ":snapshot_test", + "//testing/gtest:gtest", + ] + data = [ + "$target_gen_dir/snapshot_test.bin", + ] + snapshot_path = rebase_path(data[0], root_build_dir) + defines = [ "SNAPSHOT_PATH=\"$snapshot_path\"" ] + configs = [ ":deno_config" ] +} + +# Generates $target_gen_dir/snapshot_test.bin +snapshot("snapshot_test") { + testonly = true + source_root = "libdeno_test.js" +} diff --git a/core/libdeno/api.cc b/core/libdeno/api.cc new file mode 100644 index 000000000..fa1fe92f9 --- /dev/null +++ b/core/libdeno/api.cc @@ -0,0 +1,231 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +#include +#include +#include +#include +#include + +#include "third_party/v8/include/libplatform/libplatform.h" +#include "third_party/v8/include/v8.h" +#include "third_party/v8/src/base/logging.h" + +#include "deno.h" +#include "exceptions.h" +#include "file_util.h" +#include "internal.h" + +extern "C" { + +Deno* deno_new_snapshotter(deno_config config) { + CHECK(config.will_snapshot); + // TODO(ry) Support loading snapshots before snapshotting. + CHECK_NULL(config.load_snapshot.data_ptr); + auto* creator = new v8::SnapshotCreator(deno::external_references); + auto* isolate = creator->GetIsolate(); + auto* d = new deno::DenoIsolate(config); + d->snapshot_creator_ = creator; + d->AddIsolate(isolate); + { + v8::Locker locker(isolate); + v8::Isolate::Scope isolate_scope(isolate); + v8::HandleScope handle_scope(isolate); + auto context = v8::Context::New(isolate); + d->context_.Reset(isolate, context); + + creator->SetDefaultContext(context, + v8::SerializeInternalFieldsCallback( + deno::SerializeInternalFields, nullptr)); + deno::InitializeContext(isolate, context); + } + return reinterpret_cast(d); +} + +Deno* deno_new(deno_config config) { + if (config.will_snapshot) { + return deno_new_snapshotter(config); + } + deno::DenoIsolate* d = new deno::DenoIsolate(config); + v8::Isolate::CreateParams params; + params.array_buffer_allocator = d->array_buffer_allocator_; + params.external_references = deno::external_references; + + if (config.load_snapshot.data_ptr) { + params.snapshot_blob = &d->snapshot_; + } + + v8::Isolate* isolate = v8::Isolate::New(params); + d->AddIsolate(isolate); + + v8::Locker locker(isolate); + v8::Isolate::Scope isolate_scope(isolate); + { + v8::HandleScope handle_scope(isolate); + auto context = + v8::Context::New(isolate, nullptr, v8::MaybeLocal(), + v8::MaybeLocal(), + v8::DeserializeInternalFieldsCallback( + deno::DeserializeInternalFields, nullptr)); + if (!config.load_snapshot.data_ptr) { + // If no snapshot is provided, we initialize the context with empty + // main source code and source maps. + deno::InitializeContext(isolate, context); + } + d->context_.Reset(isolate, context); + } + + return reinterpret_cast(d); +} + +deno::DenoIsolate* unwrap(Deno* d_) { + return reinterpret_cast(d_); +} + +void deno_lock(Deno* d_) { + auto* d = unwrap(d_); + CHECK_NULL(d->locker_); + d->locker_ = new v8::Locker(d->isolate_); +} + +void deno_unlock(Deno* d_) { + auto* d = unwrap(d_); + CHECK_NOT_NULL(d->locker_); + delete d->locker_; + d->locker_ = nullptr; +} + +deno_buf deno_get_snapshot(Deno* d_) { + auto* d = unwrap(d_); + CHECK_NOT_NULL(d->snapshot_creator_); + d->ClearModules(); + d->context_.Reset(); + + auto blob = d->snapshot_creator_->CreateBlob( + v8::SnapshotCreator::FunctionCodeHandling::kKeep); + return {nullptr, 0, reinterpret_cast(const_cast(blob.data)), + blob.raw_size, 0}; +} + +static std::unique_ptr platform; + +void deno_init() { + if (platform.get() == nullptr) { + platform = v8::platform::NewDefaultPlatform(); + v8::V8::InitializePlatform(platform.get()); + v8::V8::Initialize(); + } +} + +const char* deno_v8_version() { return v8::V8::GetVersion(); } + +void deno_set_v8_flags(int* argc, char** argv) { + v8::V8::SetFlagsFromCommandLine(argc, argv, true); +} + +const char* deno_last_exception(Deno* d_) { + auto* d = unwrap(d_); + if (d->last_exception_.length() > 0) { + return d->last_exception_.c_str(); + } else { + return nullptr; + } +} + +void deno_execute(Deno* d_, void* user_data, const char* js_filename, + const char* js_source) { + auto* d = unwrap(d_); + deno::UserDataScope user_data_scope(d, user_data); + auto* isolate = d->isolate_; + v8::Locker locker(isolate); + v8::Isolate::Scope isolate_scope(isolate); + v8::HandleScope handle_scope(isolate); + auto context = d->context_.Get(d->isolate_); + CHECK(!context.IsEmpty()); + deno::Execute(context, js_filename, js_source); +} + +void deno_zero_copy_release(Deno* d_, size_t zero_copy_id) { + auto* d = unwrap(d_); + v8::Isolate::Scope isolate_scope(d->isolate_); + v8::Locker locker(d->isolate_); + v8::HandleScope handle_scope(d->isolate_); + d->DeleteZeroCopyRef(zero_copy_id); +} + +void deno_respond(Deno* d_, void* user_data, deno_buf buf) { + auto* d = unwrap(d_); + if (d->current_args_ != nullptr) { + // Synchronous response. + if (buf.data_ptr != nullptr) { + DCHECK_EQ(buf.zero_copy_id, 0); + auto ab = deno::ImportBuf(d, buf); + d->current_args_->GetReturnValue().Set(ab); + } + d->current_args_ = nullptr; + return; + } + + // Asynchronous response. + deno::UserDataScope user_data_scope(d, user_data); + v8::Locker locker(d->isolate_); + v8::Isolate::Scope isolate_scope(d->isolate_); + v8::HandleScope handle_scope(d->isolate_); + + auto context = d->context_.Get(d->isolate_); + v8::Context::Scope context_scope(context); + + v8::TryCatch try_catch(d->isolate_); + + auto recv_ = d->recv_.Get(d->isolate_); + if (recv_.IsEmpty()) { + d->last_exception_ = "Deno.core.recv has not been called."; + return; + } + + v8::Local args[1]; + int argc = 0; + + // You cannot use zero_copy_buf with deno_respond(). Use + // deno_zero_copy_release() instead. + DCHECK_EQ(buf.zero_copy_id, 0); + if (buf.data_ptr != nullptr) { + args[0] = deno::ImportBuf(d, buf); + argc = 1; + } + + auto v = recv_->Call(context, context->Global(), argc, args); + + if (try_catch.HasCaught()) { + CHECK(v.IsEmpty()); + deno::HandleException(context, try_catch.Exception()); + } +} + +void deno_check_promise_errors(Deno* d_) { + auto* d = unwrap(d_); + if (d->pending_promise_map_.size() > 0) { + auto* isolate = d->isolate_; + v8::Locker locker(isolate); + v8::Isolate::Scope isolate_scope(isolate); + v8::HandleScope handle_scope(isolate); + auto context = d->context_.Get(d->isolate_); + v8::Context::Scope context_scope(context); + + auto it = d->pending_promise_map_.begin(); + while (it != d->pending_promise_map_.end()) { + auto error = it->second.Get(isolate); + deno::HandleException(context, error); + it = d->pending_promise_map_.erase(it); + } + } +} + +void deno_delete(Deno* d_) { + deno::DenoIsolate* d = reinterpret_cast(d_); + delete d; +} + +void deno_terminate_execution(Deno* d_) { + deno::DenoIsolate* d = reinterpret_cast(d_); + d->isolate_->TerminateExecution(); +} +} diff --git a/core/libdeno/binding.cc b/core/libdeno/binding.cc new file mode 100644 index 000000000..ab633f46d --- /dev/null +++ b/core/libdeno/binding.cc @@ -0,0 +1,563 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include +#include +#endif // _WIN32 + +#include "third_party/v8/include/v8.h" +#include "third_party/v8/src/base/logging.h" + +#include "deno.h" +#include "exceptions.h" +#include "internal.h" + +#define GLOBAL_IMPORT_BUF_SIZE 1024 + +namespace deno { + +std::vector deserialized_data; + +void DeserializeInternalFields(v8::Local holder, int index, + v8::StartupData payload, void* data) { + DCHECK_NULL(data); + if (payload.raw_size == 0) { + holder->SetAlignedPointerInInternalField(index, nullptr); + return; + } + InternalFieldData* embedder_field = new InternalFieldData{0}; + memcpy(embedder_field, payload.data, payload.raw_size); + holder->SetAlignedPointerInInternalField(index, embedder_field); + deserialized_data.push_back(embedder_field); +} + +v8::StartupData SerializeInternalFields(v8::Local holder, int index, + void* data) { + DCHECK_NULL(data); + InternalFieldData* embedder_field = static_cast( + holder->GetAlignedPointerFromInternalField(index)); + if (embedder_field == nullptr) return {nullptr, 0}; + int size = sizeof(*embedder_field); + char* payload = new char[size]; + // We simply use memcpy to serialize the content. + memcpy(payload, embedder_field, size); + return {payload, size}; +} + +// Extracts a C string from a v8::V8 Utf8Value. +const char* ToCString(const v8::String::Utf8Value& value) { + return *value ? *value : ""; +} + +void PromiseRejectCallback(v8::PromiseRejectMessage promise_reject_message) { + auto* isolate = v8::Isolate::GetCurrent(); + DenoIsolate* d = static_cast(isolate->GetData(0)); + DCHECK_EQ(d->isolate_, isolate); + v8::HandleScope handle_scope(d->isolate_); + auto error = promise_reject_message.GetValue(); + auto context = d->context_.Get(d->isolate_); + auto promise = promise_reject_message.GetPromise(); + + v8::Context::Scope context_scope(context); + + int promise_id = promise->GetIdentityHash(); + switch (promise_reject_message.GetEvent()) { + case v8::kPromiseRejectWithNoHandler: + // Insert the error into the pending_promise_map_ using the promise's id + // as the key. + d->pending_promise_map_.emplace(std::piecewise_construct, + std::make_tuple(promise_id), + std::make_tuple(d->isolate_, error)); + break; + + case v8::kPromiseHandlerAddedAfterReject: + d->pending_promise_map_.erase(promise_id); + break; + + case v8::kPromiseRejectAfterResolved: + break; + + case v8::kPromiseResolveAfterResolved: + // Should not warn. See #1272 + break; + + default: + CHECK(false && "unreachable"); + } +} + +void Print(const v8::FunctionCallbackInfo& args) { + CHECK_GE(args.Length(), 1); + CHECK_LE(args.Length(), 3); + auto* isolate = args.GetIsolate(); + DenoIsolate* d = DenoIsolate::FromIsolate(isolate); + auto context = d->context_.Get(d->isolate_); + v8::HandleScope handle_scope(isolate); + bool is_err = + args.Length() >= 2 ? args[1]->BooleanValue(context).ToChecked() : false; + FILE* file = is_err ? stderr : stdout; + +#ifdef _WIN32 + int fd = _fileno(file); + if (fd < 0) return; + + HANDLE h = reinterpret_cast(_get_osfhandle(fd)); + if (h == INVALID_HANDLE_VALUE) return; + + DWORD mode; + if (GetConsoleMode(h, &mode)) { + // Print to Windows console. Since the Windows API generally doesn't support + // UTF-8 encoded text, we have to use `WriteConsoleW()` which uses UTF-16. + v8::String::Value str(isolate, args[0]); + auto str_len = static_cast(str.length()); + auto str_wchars = reinterpret_cast(*str); + + // WriteConsoleW has some limit to how many characters can be written at + // once, which is unspecified but low enough to be encountered in practice. + // Therefore we break up the write into chunks of 8kb if necessary. + size_t chunk_start = 0; + while (chunk_start < str_len) { + size_t chunk_end = std::min(chunk_start + 8192, str_len); + + // Do not break in the middle of a surrogate pair. Note that `chunk_end` + // points to the start of the next chunk, so we check whether it contains + // the second half of a surrogate pair (a.k.a. "low surrogate"). + if (chunk_end < str_len && str_wchars[chunk_end] >= 0xdc00 && + str_wchars[chunk_end] <= 0xdfff) { + --chunk_end; + } + + // Write to the console. + DWORD chunk_len = static_cast(chunk_end - chunk_start); + DWORD _; + WriteConsoleW(h, &str_wchars[chunk_start], chunk_len, &_, nullptr); + + chunk_start = chunk_end; + } + return; + } +#endif // _WIN32 + + v8::String::Utf8Value str(isolate, args[0]); + fwrite(*str, sizeof(**str), str.length(), file); + fflush(file); +} + +void ErrorToJSON(const v8::FunctionCallbackInfo& args) { + CHECK_EQ(args.Length(), 1); + auto* isolate = args.GetIsolate(); + DenoIsolate* d = DenoIsolate::FromIsolate(isolate); + auto context = d->context_.Get(d->isolate_); + v8::HandleScope handle_scope(isolate); + auto json_string = EncodeExceptionAsJSON(context, args[0]); + args.GetReturnValue().Set(v8_str(json_string.c_str())); +} + +v8::Local ImportBuf(DenoIsolate* d, deno_buf buf) { + // Do not use ImportBuf with zero_copy buffers. + DCHECK_EQ(buf.zero_copy_id, 0); + + if (buf.data_ptr == nullptr) { + return v8::Local(); + } + + if (buf.alloc_ptr == nullptr) { + // If alloc_ptr isn't set, we memcpy. + // This is currently used for flatbuffers created in Rust. + + // To avoid excessively allocating new ArrayBuffers, we try to reuse a + // single global ArrayBuffer. The caveat is that users must extract data + // from it before the next tick. We only do this for ArrayBuffers less than + // 1024 bytes. + v8::Local ab; + void* data; + if (buf.data_len > GLOBAL_IMPORT_BUF_SIZE) { + // Simple case. We allocate a new ArrayBuffer for this. + ab = v8::ArrayBuffer::New(d->isolate_, buf.data_len); + data = ab->GetContents().Data(); + } else { + // Fast case. We reuse the global ArrayBuffer. + if (d->global_import_buf_.IsEmpty()) { + // Lazily initialize it. + DCHECK_NULL(d->global_import_buf_ptr_); + ab = v8::ArrayBuffer::New(d->isolate_, GLOBAL_IMPORT_BUF_SIZE); + d->global_import_buf_.Reset(d->isolate_, ab); + d->global_import_buf_ptr_ = ab->GetContents().Data(); + } else { + DCHECK(d->global_import_buf_ptr_); + ab = d->global_import_buf_.Get(d->isolate_); + } + data = d->global_import_buf_ptr_; + } + memcpy(data, buf.data_ptr, buf.data_len); + auto view = v8::Uint8Array::New(ab, 0, buf.data_len); + return view; + } else { + auto ab = v8::ArrayBuffer::New( + d->isolate_, reinterpret_cast(buf.alloc_ptr), buf.alloc_len, + v8::ArrayBufferCreationMode::kInternalized); + auto view = + v8::Uint8Array::New(ab, buf.data_ptr - buf.alloc_ptr, buf.data_len); + return view; + } +} + +static deno_buf GetContents(v8::Isolate* isolate, + v8::Local view) { + auto ab = view->Buffer(); + auto contents = ab->GetContents(); + deno_buf buf; + buf.alloc_ptr = reinterpret_cast(contents.Data()); + buf.alloc_len = contents.ByteLength(); + buf.data_ptr = buf.alloc_ptr + view->ByteOffset(); + buf.data_len = view->ByteLength(); + return buf; +} + +// Sets the recv_ callback. +void Recv(const v8::FunctionCallbackInfo& args) { + v8::Isolate* isolate = args.GetIsolate(); + DenoIsolate* d = DenoIsolate::FromIsolate(isolate); + DCHECK_EQ(d->isolate_, isolate); + + v8::HandleScope handle_scope(isolate); + + if (!d->recv_.IsEmpty()) { + isolate->ThrowException(v8_str("Deno.core.recv already called.")); + return; + } + + v8::Local v = args[0]; + CHECK(v->IsFunction()); + v8::Local func = v8::Local::Cast(v); + + d->recv_.Reset(isolate, func); +} + +void Send(const v8::FunctionCallbackInfo& args) { + v8::Isolate* isolate = args.GetIsolate(); + DenoIsolate* d = DenoIsolate::FromIsolate(isolate); + DCHECK_EQ(d->isolate_, isolate); + + deno_buf control = {nullptr, 0u, nullptr, 0u, 0u}; + deno_buf zero_copy = {nullptr, 0u, nullptr, 0u, 0u}; + + v8::HandleScope handle_scope(isolate); + + if (args.Length() > 0) { + v8::Local control_v = args[0]; + if (control_v->IsArrayBufferView()) { + control = + GetContents(isolate, v8::Local::Cast(control_v)); + } + } + + v8::Local zero_copy_v; + if (args.Length() == 2) { + if (args[1]->IsArrayBufferView()) { + zero_copy_v = args[1]; + zero_copy = GetContents( + isolate, v8::Local::Cast(zero_copy_v)); + size_t zero_copy_id = d->next_zero_copy_id_++; + DCHECK_GT(zero_copy_id, 0); + zero_copy.zero_copy_id = zero_copy_id; + // If the zero_copy ArrayBuffer was given, we must maintain a strong + // reference to it until deno_zero_copy_release is called. + d->AddZeroCopyRef(zero_copy_id, zero_copy_v); + } + } + + DCHECK_NULL(d->current_args_); + d->current_args_ = &args; + + d->recv_cb_(d->user_data_, control, zero_copy); + + if (d->current_args_ == nullptr) { + // This indicates that deno_repond() was called already. + } else { + // Asynchronous. + d->current_args_ = nullptr; + } +} + +v8::ScriptOrigin ModuleOrigin(v8::Isolate* isolate, + v8::Local resource_name) { + return v8::ScriptOrigin(resource_name, v8::Local(), + v8::Local(), v8::Local(), + v8::Local(), v8::Local(), + v8::Local(), v8::Local(), + v8::True(isolate)); +} + +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_); + auto context = context_.Get(isolate_); + v8::Context::Scope context_scope(context); + + v8::Local name_str = v8_str(name); + v8::Local source_str = v8_str(source); + + auto origin = ModuleOrigin(isolate_, name_str); + v8::ScriptCompiler::Source source_(source_str, origin); + + v8::TryCatch try_catch(isolate_); + + auto maybe_module = v8::ScriptCompiler::CompileModule(isolate_, &source_); + + if (try_catch.HasCaught()) { + CHECK(maybe_module.IsEmpty()); + HandleException(context, try_catch.Exception()); + return 0; + } + + auto module = maybe_module.ToLocalChecked(); + + int id = module->GetIdentityHash(); + + std::vector import_specifiers; + + for (int i = 0; i < module->GetModuleRequestsLength(); ++i) { + v8::Local specifier = module->GetModuleRequest(i); + v8::String::Utf8Value specifier_utf8(isolate_, specifier); + import_specifiers.push_back(*specifier_utf8); + } + + 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; +} + +void Shared(v8::Local property, + const v8::PropertyCallbackInfo& info) { + v8::Isolate* isolate = info.GetIsolate(); + DenoIsolate* d = DenoIsolate::FromIsolate(isolate); + DCHECK_EQ(d->isolate_, isolate); + v8::Locker locker(d->isolate_); + v8::EscapableHandleScope handle_scope(isolate); + if (d->shared_.data_ptr == nullptr) { + return; + } + v8::Local ab; + if (d->shared_ab_.IsEmpty()) { + // Lazily initialize the persistent external ArrayBuffer. + ab = v8::SharedArrayBuffer::New(isolate, d->shared_.data_ptr, + d->shared_.data_len, + v8::ArrayBufferCreationMode::kExternalized); + d->shared_ab_.Reset(isolate, ab); + } + info.GetReturnValue().Set(d->shared_ab_); +} + +void DenoIsolate::ClearModules() { + for (auto it = mods_.begin(); it != mods_.end(); it++) { + it->second.handle.Reset(); + } + mods_.clear(); + mods_by_name_.clear(); +} + +bool Execute(v8::Local context, const char* js_filename, + const char* js_source) { + auto* isolate = context->GetIsolate(); + v8::Isolate::Scope isolate_scope(isolate); + v8::HandleScope handle_scope(isolate); + v8::Context::Scope context_scope(context); + + auto source = v8_str(js_source); + auto name = v8_str(js_filename); + + v8::TryCatch try_catch(isolate); + + v8::ScriptOrigin origin(name); + + auto script = v8::Script::Compile(context, source, &origin); + + if (script.IsEmpty()) { + DCHECK(try_catch.HasCaught()); + HandleException(context, try_catch.Exception()); + return false; + } + + auto result = script.ToLocalChecked()->Run(context); + + if (result.IsEmpty()) { + DCHECK(try_catch.HasCaught()); + HandleException(context, try_catch.Exception()); + return false; + } + + return true; +} + +static inline v8::Local v8_bool(bool v) { + return v8::Boolean::New(v8::Isolate::GetCurrent(), v); +} + +void EvalContext(const v8::FunctionCallbackInfo& args) { + v8::Isolate* isolate = args.GetIsolate(); + DenoIsolate* d = DenoIsolate::FromIsolate(isolate); + v8::EscapableHandleScope handleScope(isolate); + auto context = d->context_.Get(isolate); + v8::Context::Scope context_scope(context); + + CHECK(args[0]->IsString()); + auto source = args[0].As(); + + auto output = v8::Array::New(isolate, 2); + /** + * output[0] = result + * output[1] = ErrorInfo | null + * ErrorInfo = { + * thrown: Error | any, + * isNativeError: boolean, + * isCompileError: boolean, + * } + */ + + v8::TryCatch try_catch(isolate); + + auto name = v8_str(""); + v8::ScriptOrigin origin(name); + auto script = v8::Script::Compile(context, source, &origin); + + if (script.IsEmpty()) { + DCHECK(try_catch.HasCaught()); + auto exception = try_catch.Exception(); + + output->Set(0, v8::Null(isolate)); + + auto errinfo_obj = v8::Object::New(isolate); + errinfo_obj->Set(v8_str("isCompileError"), v8_bool(true)); + errinfo_obj->Set(v8_str("isNativeError"), + v8_bool(exception->IsNativeError())); + errinfo_obj->Set(v8_str("thrown"), exception); + + output->Set(1, errinfo_obj); + + args.GetReturnValue().Set(output); + return; + } + + auto result = script.ToLocalChecked()->Run(context); + + if (result.IsEmpty()) { + DCHECK(try_catch.HasCaught()); + auto exception = try_catch.Exception(); + + output->Set(0, v8::Null(isolate)); + + auto errinfo_obj = v8::Object::New(isolate); + errinfo_obj->Set(v8_str("isCompileError"), v8_bool(false)); + errinfo_obj->Set(v8_str("isNativeError"), + v8_bool(exception->IsNativeError())); + errinfo_obj->Set(v8_str("thrown"), exception); + + output->Set(1, errinfo_obj); + + args.GetReturnValue().Set(output); + return; + } + + output->Set(0, result.ToLocalChecked()); + output->Set(1, v8::Null(isolate)); + args.GetReturnValue().Set(output); +} + +void InitializeContext(v8::Isolate* isolate, v8::Local context) { + v8::HandleScope handle_scope(isolate); + v8::Context::Scope context_scope(context); + + auto global = context->Global(); + + auto deno_val = v8::Object::New(isolate); + CHECK(global->Set(context, deno::v8_str("Deno"), deno_val).FromJust()); + + auto core_val = v8::Object::New(isolate); + CHECK(deno_val->Set(context, deno::v8_str("core"), core_val).FromJust()); + + auto print_tmpl = v8::FunctionTemplate::New(isolate, Print); + auto print_val = print_tmpl->GetFunction(context).ToLocalChecked(); + CHECK(core_val->Set(context, deno::v8_str("print"), print_val).FromJust()); + + auto recv_tmpl = v8::FunctionTemplate::New(isolate, Recv); + auto recv_val = recv_tmpl->GetFunction(context).ToLocalChecked(); + CHECK(core_val->Set(context, deno::v8_str("recv"), recv_val).FromJust()); + + auto send_tmpl = v8::FunctionTemplate::New(isolate, Send); + auto send_val = send_tmpl->GetFunction(context).ToLocalChecked(); + CHECK(core_val->Set(context, deno::v8_str("send"), send_val).FromJust()); + + auto eval_context_tmpl = v8::FunctionTemplate::New(isolate, EvalContext); + auto eval_context_val = + eval_context_tmpl->GetFunction(context).ToLocalChecked(); + CHECK(core_val->Set(context, deno::v8_str("evalContext"), eval_context_val) + .FromJust()); + + auto error_to_json_tmpl = v8::FunctionTemplate::New(isolate, ErrorToJSON); + auto error_to_json_val = + error_to_json_tmpl->GetFunction(context).ToLocalChecked(); + CHECK(core_val->Set(context, deno::v8_str("errorToJSON"), error_to_json_val) + .FromJust()); + + CHECK(core_val->SetAccessor(context, deno::v8_str("shared"), Shared) + .FromJust()); +} + +void MessageCallback(v8::Local message, + v8::Local data) { + auto* isolate = message->GetIsolate(); + DenoIsolate* d = static_cast(isolate->GetData(0)); + + v8::HandleScope handle_scope(isolate); + auto context = d->context_.Get(isolate); + HandleExceptionMessage(context, message); +} + +void HostInitializeImportMetaObjectCallback(v8::Local context, + v8::Local module, + v8::Local meta) { + auto* isolate = context->GetIsolate(); + DenoIsolate* d = DenoIsolate::FromIsolate(isolate); + v8::Isolate::Scope isolate_scope(isolate); + + CHECK(!module.IsEmpty()); + + deno_mod id = module->GetIdentityHash(); + CHECK_NE(id, 0); + + 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) { + isolate_ = isolate; + isolate_->SetCaptureStackTraceForUncaughtExceptions( + true, 10, v8::StackTrace::kDetailed); + isolate_->SetPromiseRejectCallback(deno::PromiseRejectCallback); + isolate_->SetData(0, this); + isolate_->AddMessageListener(MessageCallback); + isolate->SetHostInitializeImportMetaObjectCallback( + HostInitializeImportMetaObjectCallback); +} + +} // namespace deno diff --git a/core/libdeno/deno.h b/core/libdeno/deno.h new file mode 100644 index 000000000..f3902985e --- /dev/null +++ b/core/libdeno/deno.h @@ -0,0 +1,112 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +#ifndef DENO_H_ +#define DENO_H_ +#include +#include +// Neither Rust nor Go support calling directly into C++ functions, therefore +// the public interface to libdeno is done in C. +#ifdef __cplusplus +extern "C" { +#endif + +// Data that gets transmitted. +typedef struct { + uint8_t* alloc_ptr; // Start of memory allocation (returned from `malloc()`). + size_t alloc_len; // Length of the memory allocation. + uint8_t* data_ptr; // Start of logical contents (within the allocation). + size_t data_len; // Length of logical contents. + size_t zero_copy_id; // 0 = normal, 1 = must call deno_zero_copy_release. +} deno_buf; + +typedef struct deno_s Deno; + +// A callback to receive a message from a libdeno.send() javascript call. +// control_buf is valid for only for the lifetime of this callback. +// data_buf is valid until deno_respond() is called. +typedef void (*deno_recv_cb)(void* user_data, deno_buf control_buf, + deno_buf zerop_copy_buf); + +void deno_init(); +const char* deno_v8_version(); +void deno_set_v8_flags(int* argc, char** argv); + +typedef struct { + int will_snapshot; // Default 0. If calling deno_get_snapshot 1. + deno_buf load_snapshot; // Optionally: A deno_buf from deno_get_snapshot. + deno_buf shared; // Shared buffer to be mapped to libdeno.shared + deno_recv_cb recv_cb; // Maps to libdeno.send() calls. +} deno_config; + +// Create a new deno isolate. +// Warning: If config.will_snapshot is set, deno_get_snapshot() must be called +// or an error will result. +Deno* deno_new(deno_config config); + +// Generate a snapshot. The resulting buf can be used with deno_new. +// The caller must free the returned data by calling delete[] buf.data_ptr. +deno_buf deno_get_snapshot(Deno* d); + +void deno_delete(Deno* d); + +void deno_lock(Deno* d); +void deno_unlock(Deno* d); + +// Compile and execute a traditional JavaScript script that does not use +// module import statements. +// If it succeeded deno_last_exception() will return NULL. +void deno_execute(Deno* d, void* user_data, const char* js_filename, + const char* js_source); + +// deno_respond sends up to one message back for every deno_recv_cb made. +// +// If this is called during deno_recv_cb, the issuing libdeno.send() in +// javascript will synchronously return the specified buf as an ArrayBuffer (or +// null if buf is empty). +// +// If this is called after deno_recv_cb has returned, the deno_respond +// will call into the JS callback specified by libdeno.recv(). +// +// (Ideally, but not currently: After calling deno_respond(), the caller no +// longer owns `buf` and must not use it; deno_respond() is responsible for +// releasing its memory.) +// +// If a JS exception was encountered, deno_last_exception() will be non-NULL. +void deno_respond(Deno* d, void* user_data, deno_buf buf); + +// consumes zero_copy +// Calling this function more than once with the same zero_copy_id will result +// in an error. +void deno_zero_copy_release(Deno* d, size_t zero_copy_id); + +void deno_check_promise_errors(Deno* d); + +const char* deno_last_exception(Deno* d); + +void deno_terminate_execution(Deno* d); + +// Module API + +typedef int deno_mod; + +// Returns zero on error - check deno_last_exception(). +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); + +// Returned pointer is valid for the lifetime of the Deno isolate "d". +const char* deno_mod_imports_get(Deno* d, deno_mod id, size_t index); + +typedef deno_mod (*deno_resolve_cb)(void* user_data, const char* specifier, + deno_mod referrer); + +// If it succeeded deno_last_exception() will return NULL. +void deno_mod_instantiate(Deno* d, void* user_data, deno_mod id, + deno_resolve_cb cb); + +// If it succeeded deno_last_exception() will return NULL. +void deno_mod_evaluate(Deno* d, void* user_data, deno_mod id); + +#ifdef __cplusplus +} // extern "C" +#endif +#endif // DENO_H_ diff --git a/core/libdeno/exceptions.cc b/core/libdeno/exceptions.cc new file mode 100644 index 000000000..85f0ca340 --- /dev/null +++ b/core/libdeno/exceptions.cc @@ -0,0 +1,217 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +#include "exceptions.h" +#include + +namespace deno { + +v8::Local EncodeMessageAsObject(v8::Local context, + v8::Local message) { + auto* isolate = context->GetIsolate(); + v8::EscapableHandleScope handle_scope(isolate); + v8::Context::Scope context_scope(context); + + auto stack_trace = message->GetStackTrace(); + + // Encode the exception into a JS object, which we will then turn into JSON. + auto json_obj = v8::Object::New(isolate); + auto exception_str = message->Get(); + CHECK(json_obj->Set(context, v8_str("message"), exception_str).FromJust()); + + auto maybe_source_line = message->GetSourceLine(context); + if (!maybe_source_line.IsEmpty()) { + CHECK(json_obj + ->Set(context, v8_str("sourceLine"), + maybe_source_line.ToLocalChecked()) + .FromJust()); + } + + CHECK(json_obj + ->Set(context, v8_str("scriptResourceName"), + message->GetScriptResourceName()) + .FromJust()); + + auto maybe_line_number = message->GetLineNumber(context); + if (maybe_line_number.IsJust()) { + CHECK(json_obj + ->Set(context, v8_str("lineNumber"), + v8::Integer::New(isolate, maybe_line_number.FromJust())) + .FromJust()); + } + + CHECK(json_obj + ->Set(context, v8_str("startPosition"), + v8::Integer::New(isolate, message->GetStartPosition())) + .FromJust()); + + CHECK(json_obj + ->Set(context, v8_str("endPosition"), + v8::Integer::New(isolate, message->GetEndPosition())) + .FromJust()); + + CHECK(json_obj + ->Set(context, v8_str("errorLevel"), + v8::Integer::New(isolate, message->ErrorLevel())) + .FromJust()); + + auto maybe_start_column = message->GetStartColumn(context); + if (maybe_start_column.IsJust()) { + auto start_column = + v8::Integer::New(isolate, maybe_start_column.FromJust()); + CHECK( + json_obj->Set(context, v8_str("startColumn"), start_column).FromJust()); + } + + auto maybe_end_column = message->GetEndColumn(context); + if (maybe_end_column.IsJust()) { + auto end_column = v8::Integer::New(isolate, maybe_end_column.FromJust()); + CHECK(json_obj->Set(context, v8_str("endColumn"), end_column).FromJust()); + } + + CHECK(json_obj + ->Set(context, v8_str("isSharedCrossOrigin"), + v8::Boolean::New(isolate, message->IsSharedCrossOrigin())) + .FromJust()); + + CHECK(json_obj + ->Set(context, v8_str("isOpaque"), + v8::Boolean::New(isolate, message->IsOpaque())) + .FromJust()); + + v8::Local frames; + if (!stack_trace.IsEmpty()) { + uint32_t count = static_cast(stack_trace->GetFrameCount()); + frames = v8::Array::New(isolate, count); + + for (uint32_t i = 0; i < count; ++i) { + auto frame = stack_trace->GetFrame(isolate, i); + auto frame_obj = v8::Object::New(isolate); + CHECK(frames->Set(context, i, frame_obj).FromJust()); + auto line = v8::Integer::New(isolate, frame->GetLineNumber()); + auto column = v8::Integer::New(isolate, frame->GetColumn()); + CHECK(frame_obj->Set(context, v8_str("line"), line).FromJust()); + CHECK(frame_obj->Set(context, v8_str("column"), column).FromJust()); + CHECK(frame_obj + ->Set(context, v8_str("functionName"), frame->GetFunctionName()) + .FromJust()); + // scriptName can be empty in special conditions e.g. eval + auto scriptName = frame->GetScriptNameOrSourceURL(); + if (scriptName.IsEmpty()) { + scriptName = v8_str(""); + } + CHECK( + frame_obj->Set(context, v8_str("scriptName"), scriptName).FromJust()); + CHECK(frame_obj + ->Set(context, v8_str("isEval"), + v8::Boolean::New(isolate, frame->IsEval())) + .FromJust()); + CHECK(frame_obj + ->Set(context, v8_str("isConstructor"), + v8::Boolean::New(isolate, frame->IsConstructor())) + .FromJust()); + CHECK(frame_obj + ->Set(context, v8_str("isWasm"), + v8::Boolean::New(isolate, frame->IsWasm())) + .FromJust()); + } + } else { + // No stack trace. We only have one stack frame of info.. + frames = v8::Array::New(isolate, 1); + + auto frame_obj = v8::Object::New(isolate); + CHECK(frames->Set(context, 0, frame_obj).FromJust()); + + auto line = + v8::Integer::New(isolate, message->GetLineNumber(context).FromJust()); + auto column = + v8::Integer::New(isolate, message->GetStartColumn(context).FromJust()); + + CHECK(frame_obj->Set(context, v8_str("line"), line).FromJust()); + CHECK(frame_obj->Set(context, v8_str("column"), column).FromJust()); + CHECK(frame_obj + ->Set(context, v8_str("scriptName"), + message->GetScriptResourceName()) + .FromJust()); + } + + CHECK(json_obj->Set(context, v8_str("frames"), frames).FromJust()); + json_obj = handle_scope.Escape(json_obj); + return json_obj; +} + +std::string EncodeMessageAsJSON(v8::Local context, + v8::Local message) { + auto* isolate = context->GetIsolate(); + v8::HandleScope handle_scope(isolate); + v8::Context::Scope context_scope(context); + auto json_obj = EncodeMessageAsObject(context, message); + auto json_string = v8::JSON::Stringify(context, json_obj).ToLocalChecked(); + v8::String::Utf8Value json_string_(isolate, json_string); + return std::string(ToCString(json_string_)); +} + +v8::Local EncodeExceptionAsObject(v8::Local context, + v8::Local exception) { + auto* isolate = context->GetIsolate(); + v8::EscapableHandleScope handle_scope(isolate); + v8::Context::Scope context_scope(context); + + auto message = v8::Exception::CreateMessage(isolate, exception); + auto json_obj = EncodeMessageAsObject(context, message); + json_obj = handle_scope.Escape(json_obj); + return json_obj; +} + +std::string EncodeExceptionAsJSON(v8::Local context, + v8::Local exception) { + auto* isolate = context->GetIsolate(); + v8::HandleScope handle_scope(isolate); + v8::Context::Scope context_scope(context); + + auto message = v8::Exception::CreateMessage(isolate, exception); + return EncodeMessageAsJSON(context, message); +} + +void HandleException(v8::Local context, + v8::Local exception) { + v8::Isolate* isolate = context->GetIsolate(); + + // TerminateExecution was called + if (isolate->IsExecutionTerminating()) { + // cancel exception termination so that the exception can be created + isolate->CancelTerminateExecution(); + + // maybe make a new exception object + if (exception->IsNullOrUndefined()) { + exception = v8::Exception::Error(v8_str("execution terminated")); + } + + // handle the exception as if it is a regular exception + HandleException(context, exception); + + // re-enable exception termination + isolate->TerminateExecution(); + return; + } + + DenoIsolate* d = DenoIsolate::FromIsolate(isolate); + std::string json_str = EncodeExceptionAsJSON(context, exception); + CHECK_NOT_NULL(d); + d->last_exception_ = json_str; +} + +void HandleExceptionMessage(v8::Local context, + v8::Local message) { + v8::Isolate* isolate = context->GetIsolate(); + + // TerminateExecution was called + if (isolate->IsExecutionTerminating()) { + HandleException(context, v8::Undefined(isolate)); + return; + } + + DenoIsolate* d = DenoIsolate::FromIsolate(isolate); + std::string json_str = EncodeMessageAsJSON(context, message); + CHECK_NOT_NULL(d); + d->last_exception_ = json_str; +} +} // namespace deno diff --git a/core/libdeno/exceptions.h b/core/libdeno/exceptions.h new file mode 100644 index 000000000..e07ff183a --- /dev/null +++ b/core/libdeno/exceptions.h @@ -0,0 +1,23 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +#ifndef EXCEPTIONS_H_ +#define EXCEPTIONS_H_ + +#include +#include "third_party/v8/include/v8.h" + +namespace deno { + +v8::Local EncodeExceptionAsObject(v8::Local context, + v8::Local exception); + +std::string EncodeExceptionAsJSON(v8::Local context, + v8::Local exception); + +void HandleException(v8::Local context, + v8::Local exception); + +void HandleExceptionMessage(v8::Local context, + v8::Local message); +} // namespace deno + +#endif // EXCEPTIONS_H_ diff --git a/core/libdeno/file_util.cc b/core/libdeno/file_util.cc new file mode 100644 index 000000000..256f4f257 --- /dev/null +++ b/core/libdeno/file_util.cc @@ -0,0 +1,90 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __APPLE__ +#include +#endif + +#ifdef _WIN32 +#include +#endif + +#include "file_util.h" + +namespace deno { + +bool ReadFileToString(const char* fn, std::string* contents) { + std::ifstream file(fn, std::ios::binary); + if (file.fail()) { + return false; + } + contents->assign(std::istreambuf_iterator{file}, {}); + return !file.fail(); +} + +std::string Basename(std::string const& filename) { + for (auto it = filename.rbegin(); it != filename.rend(); ++it) { + char ch = *it; + if (ch == '\\' || ch == '/') { + return std::string(it.base(), filename.end()); + } + } + return filename; +} + +// Returns the directory component from a filename. The returned path always +// ends with a slash. This function does not understand Windows drive letters. +std::string Dirname(std::string const& filename) { + for (auto it = filename.rbegin(); it != filename.rend(); ++it) { + char ch = *it; + if (ch == '\\' || ch == '/') { + return std::string(filename.begin(), it.base()); + } + } + return std::string("./"); +} + +// Returns the full path the currently running executable. +// This implementation is very basic. Caveats: +// * OS X: fails if buffer is too small, does not retry with a bigger buffer. +// * Windows: ANSI only, no unicode. Fails if path is longer than 260 chars. +bool ExePath(std::string* path) { +#ifdef _WIN32 + // Windows only. + char exe_buf[MAX_PATH]; + DWORD len = GetModuleFileNameA(NULL, exe_buf, sizeof exe_buf); + if (len == 0 || len == sizeof exe_buf) { + return false; + } +#else +#ifdef __APPLE__ + // OS X only. + char link_buf[PATH_MAX * 2]; // Exe may be longer than MAX_PATH, says Apple. + uint32_t len = sizeof link_buf; + if (_NSGetExecutablePath(link_buf, &len) < 0) { + return false; + } +#else + // Linux only. + static const char* link_buf = "/proc/self/exe"; +#endif + // Linux and OS X. + char exe_buf[PATH_MAX]; + char* r = realpath(link_buf, exe_buf); + if (r == NULL) { + return false; + } +#endif + // All platforms. + path->assign(exe_buf); + return true; +} + +} // namespace deno diff --git a/core/libdeno/file_util.h b/core/libdeno/file_util.h new file mode 100644 index 000000000..0177f2c94 --- /dev/null +++ b/core/libdeno/file_util.h @@ -0,0 +1,14 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +#ifndef FILE_UTIL_H_ +#define FILE_UTIL_H_ + +#include + +namespace deno { +bool ReadFileToString(const char* fn, std::string* contents); +std::string Basename(std::string const& filename); +std::string Dirname(std::string const& filename); +bool ExePath(std::string* path); +} // namespace deno + +#endif // FILE_UTIL_H_ diff --git a/core/libdeno/file_util_test.cc b/core/libdeno/file_util_test.cc new file mode 100644 index 000000000..80c878044 --- /dev/null +++ b/core/libdeno/file_util_test.cc @@ -0,0 +1,46 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +#include "testing/gtest/include/gtest/gtest.h" + +#include "file_util.h" + +TEST(FileUtilTest, ReadFileToStringFileNotExist) { + std::string output; + EXPECT_FALSE(deno::ReadFileToString("/should_error_out.txt", &output)); +} + +TEST(FileUtilTest, Basename) { + EXPECT_EQ("foo.txt", deno::Basename("foo.txt")); + EXPECT_EQ("foo.txt", deno::Basename("/foo.txt")); + EXPECT_EQ("", deno::Basename("/foo/")); + EXPECT_EQ("", deno::Basename("foo/")); + EXPECT_EQ("", deno::Basename("/")); + EXPECT_EQ("foo.txt", deno::Basename(".\\foo.txt")); + EXPECT_EQ("foo.txt", deno::Basename("/home/ryan/foo.txt")); + EXPECT_EQ("foo.txt", deno::Basename("C:\\home\\ryan\\foo.txt")); +} + +TEST(FileUtilTest, Dirname) { + EXPECT_EQ("home/dank/", deno::Dirname("home/dank/memes.gif")); + EXPECT_EQ("/home/dank/", deno::Dirname("/home/dank/memes.gif")); + EXPECT_EQ("/home/dank/", deno::Dirname("/home/dank/")); + EXPECT_EQ("home/dank/", deno::Dirname("home/dank/memes.gif")); + EXPECT_EQ("/", deno::Dirname("/")); + EXPECT_EQ(".\\", deno::Dirname(".\\memes.gif")); + EXPECT_EQ("c:\\", deno::Dirname("c:\\stuff")); + EXPECT_EQ("./", deno::Dirname("nothing")); + EXPECT_EQ("./", deno::Dirname("")); +} + +TEST(FileUtilTest, ExePath) { + std::string exe_path; + EXPECT_TRUE(deno::ExePath(&exe_path)); + // Path is absolute. + EXPECT_TRUE(exe_path.find("/") == 0 || exe_path.find(":\\") == 1); + // FIlename is the name of the test binary. + std::string exe_filename = deno::Basename(exe_path); + EXPECT_EQ(exe_filename.find("test_cc"), 0u); + // Path exists (also tests ReadFileToString). + std::string contents; + EXPECT_TRUE(deno::ReadFileToString(exe_path.c_str(), &contents)); + EXPECT_NE(contents.find("Inception :)"), std::string::npos); +} diff --git a/core/libdeno/internal.h b/core/libdeno/internal.h new file mode 100644 index 000000000..71cf731b6 --- /dev/null +++ b/core/libdeno/internal.h @@ -0,0 +1,199 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +#ifndef INTERNAL_H_ +#define INTERNAL_H_ + +#include +#include +#include +#include +#include "deno.h" +#include "third_party/v8/include/v8.h" +#include "third_party/v8/src/base/logging.h" + +namespace deno { + +struct ModuleInfo { + bool main; + std::string name; + v8::Persistent handle; + std::vector import_specifiers; + + ModuleInfo(v8::Isolate* isolate, v8::Local module, bool main_, + const char* name_, std::vector import_specifiers_) + : main(main_), name(name_), import_specifiers(import_specifiers_) { + handle.Reset(isolate, module); + } +}; + +// deno_s = Wrapped Isolate. +class DenoIsolate { + public: + explicit DenoIsolate(deno_config config) + : isolate_(nullptr), + locker_(nullptr), + shared_(config.shared), + current_args_(nullptr), + snapshot_creator_(nullptr), + global_import_buf_ptr_(nullptr), + recv_cb_(config.recv_cb), + next_zero_copy_id_(1), // zero_copy_id must not be zero. + user_data_(nullptr), + resolve_cb_(nullptr) { + array_buffer_allocator_ = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); + if (config.load_snapshot.data_ptr) { + snapshot_.data = + reinterpret_cast(config.load_snapshot.data_ptr); + snapshot_.raw_size = static_cast(config.load_snapshot.data_len); + } + } + + ~DenoIsolate() { + shared_ab_.Reset(); + if (locker_) { + delete locker_; + } + if (snapshot_creator_) { + delete snapshot_creator_; + } else { + isolate_->Dispose(); + } + delete array_buffer_allocator_; + } + + static inline DenoIsolate* FromIsolate(v8::Isolate* isolate) { + return static_cast(isolate->GetData(0)); + } + + void AddIsolate(v8::Isolate* isolate); + + deno_mod RegisterModule(bool main, const char* name, const char* source); + void ClearModules(); + + ModuleInfo* GetModuleInfo(deno_mod id) { + if (id == 0) { + return nullptr; + } + auto it = mods_.find(id); + if (it != mods_.end()) { + return &it->second; + } else { + return nullptr; + } + } + + void DeleteZeroCopyRef(size_t zero_copy_id) { + DCHECK_NE(zero_copy_id, 0); + // Delete persistent reference to data ArrayBuffer. + auto it = zero_copy_map_.find(zero_copy_id); + if (it != zero_copy_map_.end()) { + it->second.Reset(); + zero_copy_map_.erase(it); + } + } + + void AddZeroCopyRef(size_t zero_copy_id, v8::Local zero_copy_v) { + zero_copy_map_.emplace(std::piecewise_construct, + std::make_tuple(zero_copy_id), + std::make_tuple(isolate_, zero_copy_v)); + } + + v8::Isolate* isolate_; + v8::Locker* locker_; + v8::ArrayBuffer::Allocator* array_buffer_allocator_; + deno_buf shared_; + const v8::FunctionCallbackInfo* current_args_; + v8::SnapshotCreator* snapshot_creator_; + void* global_import_buf_ptr_; + deno_recv_cb recv_cb_; + size_t next_zero_copy_id_; + void* user_data_; + + std::map mods_; + std::map mods_by_name_; + deno_resolve_cb resolve_cb_; + + v8::Persistent context_; + std::map> zero_copy_map_; + std::map> pending_promise_map_; + std::string last_exception_; + v8::Persistent recv_; + v8::StartupData snapshot_; + v8::Persistent global_import_buf_; + v8::Persistent shared_ab_; +}; + +class UserDataScope { + DenoIsolate* deno_; + void* prev_data_; + void* data_; // Not necessary; only for sanity checking. + + public: + UserDataScope(DenoIsolate* deno, void* data) : deno_(deno), data_(data) { + CHECK(deno->user_data_ == nullptr || deno->user_data_ == data_); + prev_data_ = deno->user_data_; + deno->user_data_ = data; + } + + ~UserDataScope() { + CHECK(deno_->user_data_ == data_); + deno_->user_data_ = prev_data_; + } +}; + +struct InternalFieldData { + uint32_t data; +}; + +static inline v8::Local v8_str(const char* x) { + return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x, + v8::NewStringType::kNormal) + .ToLocalChecked(); +} + +void Print(const v8::FunctionCallbackInfo& args); +void Recv(const v8::FunctionCallbackInfo& args); +void Send(const v8::FunctionCallbackInfo& args); +void EvalContext(const v8::FunctionCallbackInfo& args); +void ErrorToJSON(const v8::FunctionCallbackInfo& args); +void Shared(v8::Local property, + const v8::PropertyCallbackInfo& info); +void MessageCallback(v8::Local message, v8::Local data); +static intptr_t external_references[] = { + reinterpret_cast(Print), + reinterpret_cast(Recv), + reinterpret_cast(Send), + reinterpret_cast(EvalContext), + reinterpret_cast(ErrorToJSON), + reinterpret_cast(Shared), + reinterpret_cast(MessageCallback), + 0}; + +static const deno_buf empty_buf = {nullptr, 0, nullptr, 0, 0}; + +Deno* NewFromSnapshot(void* user_data, deno_recv_cb cb); + +void InitializeContext(v8::Isolate* isolate, v8::Local context); + +void DeserializeInternalFields(v8::Local holder, int index, + v8::StartupData payload, void* data); + +v8::StartupData SerializeInternalFields(v8::Local holder, int index, + void* data); + +v8::Local ImportBuf(DenoIsolate* d, deno_buf buf); + +bool Execute(v8::Local context, const char* js_filename, + const char* js_source); +bool ExecuteMod(v8::Local context, const char* js_filename, + const char* js_source, bool resolve_only); + +} // namespace deno + +extern "C" { +// This is just to workaround the linker. +struct deno_s { + deno::DenoIsolate isolate; +}; +} + +#endif // INTERNAL_H_ diff --git a/core/libdeno/libdeno.d.ts b/core/libdeno/libdeno.d.ts new file mode 100644 index 000000000..1bc7367d9 --- /dev/null +++ b/core/libdeno/libdeno.d.ts @@ -0,0 +1,40 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. + +interface EvalErrorInfo { + // Is the object thrown a native Error? + isNativeError: boolean; + // Was the error happened during compilation? + isCompileError: boolean; + // The actual thrown entity + // (might be an Error or anything else thrown by the user) + // If isNativeError is true, this is an Error + // eslint-disable-next-line @typescript-eslint/no-explicit-any + thrown: any; +} + +declare interface MessageCallback { + (msg: Uint8Array): void; +} + +declare interface DenoCore { + recv(cb: MessageCallback): void; + + send( + control: null | ArrayBufferView, + data?: ArrayBufferView + ): null | Uint8Array; + + print(x: string, isErr?: boolean): void; + + shared: SharedArrayBuffer; + + /** 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]. + * If an error occurs, `output` becomes null and `errInfo` is non-null. + */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any + evalContext(code: string): [any, EvalErrorInfo | null]; + + errorToJSON: (e: Error) => string; +} diff --git a/core/libdeno/libdeno_test.cc b/core/libdeno/libdeno_test.cc new file mode 100644 index 000000000..8949e7f4a --- /dev/null +++ b/core/libdeno/libdeno_test.cc @@ -0,0 +1,329 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +#include "test.h" + +TEST(LibDenoTest, InitializesCorrectly) { + EXPECT_NE(snapshot.data_ptr, nullptr); + Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); + deno_execute(d, nullptr, "a.js", "1 + 2"); + EXPECT_EQ(nullptr, deno_last_exception(d)); + deno_delete(d); +} + +TEST(LibDenoTest, Snapshotter) { + Deno* d1 = deno_new(deno_config{1, empty, empty, nullptr}); + deno_execute(d1, nullptr, "a.js", "a = 1 + 2"); + EXPECT_EQ(nullptr, deno_last_exception(d1)); + deno_buf test_snapshot = deno_get_snapshot(d1); + deno_delete(d1); + + Deno* d2 = deno_new(deno_config{0, test_snapshot, empty, nullptr}); + deno_execute(d2, nullptr, "b.js", "if (a != 3) throw Error('x');"); + EXPECT_EQ(nullptr, deno_last_exception(d2)); + deno_delete(d2); + + delete[] test_snapshot.data_ptr; +} + +TEST(LibDenoTest, CanCallFunction) { + Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); + deno_lock(d); + deno_execute(d, nullptr, "a.js", + "if (CanCallFunction() != 'foo') throw Error();"); + EXPECT_EQ(nullptr, deno_last_exception(d)); + deno_unlock(d); + deno_delete(d); +} + +TEST(LibDenoTest, ErrorsCorrectly) { + Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); + deno_execute(d, nullptr, "a.js", "throw Error()"); + EXPECT_NE(nullptr, deno_last_exception(d)); + deno_delete(d); +} + +deno_buf strbuf(const char* str) { + auto len = strlen(str); + + deno_buf buf; + buf.alloc_ptr = reinterpret_cast(strdup(str)); + buf.alloc_len = len + 1; + buf.data_ptr = buf.alloc_ptr; + buf.data_len = len; + buf.zero_copy_id = 0; + + return buf; +} + +// Same as strbuf but with null alloc_ptr. +deno_buf StrBufNullAllocPtr(const char* str) { + auto len = strlen(str); + deno_buf buf; + buf.alloc_ptr = nullptr; + buf.alloc_len = 0; + buf.data_ptr = reinterpret_cast(strdup(str)); + buf.data_len = len; + return buf; +} + +void assert_null(deno_buf b) { + EXPECT_EQ(b.alloc_ptr, nullptr); + EXPECT_EQ(b.alloc_len, 0u); + EXPECT_EQ(b.data_ptr, nullptr); + EXPECT_EQ(b.data_len, 0u); +} + +TEST(LibDenoTest, RecvReturnEmpty) { + static int count = 0; + auto recv_cb = [](auto _, auto buf, auto zero_copy_buf) { + assert_null(zero_copy_buf); + count++; + EXPECT_EQ(static_cast(3), buf.data_len); + EXPECT_EQ(buf.data_ptr[0], 'a'); + EXPECT_EQ(buf.data_ptr[1], 'b'); + EXPECT_EQ(buf.data_ptr[2], 'c'); + }; + Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb}); + deno_execute(d, nullptr, "a.js", "RecvReturnEmpty()"); + EXPECT_EQ(nullptr, deno_last_exception(d)); + EXPECT_EQ(count, 2); + deno_delete(d); +} + +TEST(LibDenoTest, RecvReturnBar) { + static int count = 0; + auto recv_cb = [](auto user_data, auto buf, auto zero_copy_buf) { + auto d = reinterpret_cast(user_data); + assert_null(zero_copy_buf); + count++; + EXPECT_EQ(static_cast(3), buf.data_len); + EXPECT_EQ(buf.data_ptr[0], 'a'); + EXPECT_EQ(buf.data_ptr[1], 'b'); + EXPECT_EQ(buf.data_ptr[2], 'c'); + EXPECT_EQ(zero_copy_buf.zero_copy_id, 0u); + EXPECT_EQ(zero_copy_buf.data_ptr, nullptr); + deno_respond(d, user_data, strbuf("bar")); + }; + Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb}); + deno_execute(d, d, "a.js", "RecvReturnBar()"); + EXPECT_EQ(nullptr, deno_last_exception(d)); + EXPECT_EQ(count, 1); + deno_delete(d); +} + +TEST(LibDenoTest, DoubleRecvFails) { + Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); + deno_execute(d, nullptr, "a.js", "DoubleRecvFails()"); + EXPECT_NE(nullptr, deno_last_exception(d)); + deno_delete(d); +} + +TEST(LibDenoTest, SendRecvSlice) { + static int count = 0; + auto recv_cb = [](auto user_data, auto buf, auto zero_copy_buf) { + auto d = reinterpret_cast(user_data); + assert_null(zero_copy_buf); + static const size_t alloc_len = 1024; + size_t i = count++; + // Check the size and offset of the slice. + size_t data_offset = buf.data_ptr - buf.alloc_ptr; + EXPECT_EQ(data_offset, i * 11); + EXPECT_EQ(buf.data_len, alloc_len - i * 30); + EXPECT_EQ(buf.alloc_len, alloc_len); + // Check values written by the JS side. + EXPECT_EQ(buf.data_ptr[0], 100 + i); + EXPECT_EQ(buf.data_ptr[buf.data_len - 1], 100 - i); + // Make copy of the backing buffer -- this is currently necessary + // because deno_respond() takes ownership over the buffer, but we are + // not given ownership of `buf` by our caller. + uint8_t* alloc_ptr = reinterpret_cast(malloc(alloc_len)); + memcpy(alloc_ptr, buf.alloc_ptr, alloc_len); + // Make a slice that is a bit shorter than the original. + deno_buf buf2{alloc_ptr, alloc_len, alloc_ptr + data_offset, + buf.data_len - 19, 0}; + // Place some values into the buffer for the JS side to verify. + buf2.data_ptr[0] = 200 + i; + buf2.data_ptr[buf2.data_len - 1] = 200 - i; + // Send back. + deno_respond(d, user_data, buf2); + }; + Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb}); + deno_execute(d, d, "a.js", "SendRecvSlice()"); + EXPECT_EQ(nullptr, deno_last_exception(d)); + EXPECT_EQ(count, 5); + deno_delete(d); +} + +TEST(LibDenoTest, JSSendArrayBufferViewTypes) { + static int count = 0; + auto recv_cb = [](auto _, auto buf, auto zero_copy_buf) { + assert_null(zero_copy_buf); + count++; + size_t data_offset = buf.data_ptr - buf.alloc_ptr; + EXPECT_EQ(data_offset, 2468u); + EXPECT_EQ(buf.data_len, 1000u); + EXPECT_EQ(buf.alloc_len, 4321u); + EXPECT_EQ(buf.data_ptr[0], count); + }; + Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb}); + deno_execute(d, nullptr, "a.js", "JSSendArrayBufferViewTypes()"); + EXPECT_EQ(nullptr, deno_last_exception(d)); + EXPECT_EQ(count, 3); + deno_delete(d); +} + +TEST(LibDenoTest, TypedArraySnapshots) { + Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); + deno_execute(d, nullptr, "a.js", "TypedArraySnapshots()"); + EXPECT_EQ(nullptr, deno_last_exception(d)); + deno_delete(d); +} + +TEST(LibDenoTest, SnapshotBug) { + Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); + deno_execute(d, nullptr, "a.js", "SnapshotBug()"); + EXPECT_EQ(nullptr, deno_last_exception(d)); + deno_delete(d); +} + +TEST(LibDenoTest, GlobalErrorHandling) { + Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); + deno_execute(d, nullptr, "a.js", "GlobalErrorHandling()"); + std::string expected = + "{\"message\":\"Uncaught ReferenceError: notdefined is not defined\"," + "\"sourceLine\":\" " + "notdefined()\",\"scriptResourceName\":\"helloworld.js\"," + "\"lineNumber\":3,\"startPosition\":3,\"endPosition\":4,\"errorLevel\":8," + "\"startColumn\":1,\"endColumn\":2,\"isSharedCrossOrigin\":false," + "\"isOpaque\":false,\"frames\":[{\"line\":3,\"column\":2," + "\"functionName\":\"\",\"scriptName\":\"helloworld.js\",\"isEval\":true," + "\"isConstructor\":false,\"isWasm\":false},"; + std::string actual(deno_last_exception(d), 0, expected.length()); + EXPECT_STREQ(expected.c_str(), actual.c_str()); + deno_delete(d); +} + +TEST(LibDenoTest, ZeroCopyBuf) { + static int count = 0; + static deno_buf zero_copy_buf2; + auto recv_cb = [](auto user_data, deno_buf buf, deno_buf zero_copy_buf) { + count++; + EXPECT_GT(zero_copy_buf.zero_copy_id, 0u); + zero_copy_buf.data_ptr[0] = 4; + zero_copy_buf.data_ptr[1] = 2; + zero_copy_buf2 = zero_copy_buf; + EXPECT_EQ(2u, buf.data_len); + EXPECT_EQ(2u, zero_copy_buf.data_len); + EXPECT_EQ(buf.data_ptr[0], 1); + EXPECT_EQ(buf.data_ptr[1], 2); + // Note zero_copy_buf won't actually be freed here because in + // libdeno_test.js zeroCopyBuf is a rooted global. We just want to exercise + // the API here. + auto d = reinterpret_cast(user_data); + deno_zero_copy_release(d, zero_copy_buf.zero_copy_id); + }; + Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb}); + deno_execute(d, d, "a.js", "ZeroCopyBuf()"); + EXPECT_EQ(nullptr, deno_last_exception(d)); + EXPECT_EQ(count, 1); + // zero_copy_buf was subsequently changed in JS, let's check that our copy + // reflects that. + EXPECT_EQ(zero_copy_buf2.data_ptr[0], 9); + EXPECT_EQ(zero_copy_buf2.data_ptr[1], 8); + deno_delete(d); +} + +TEST(LibDenoTest, CheckPromiseErrors) { + static int count = 0; + auto recv_cb = [](auto _, auto buf, auto zero_copy_buf) { count++; }; + Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb}); + EXPECT_EQ(deno_last_exception(d), nullptr); + deno_execute(d, nullptr, "a.js", "CheckPromiseErrors()"); + EXPECT_EQ(nullptr, deno_last_exception(d)); + EXPECT_EQ(deno_last_exception(d), nullptr); + EXPECT_EQ(count, 1); + // We caught the exception. So still no errors after calling + // deno_check_promise_errors(). + deno_check_promise_errors(d); + EXPECT_EQ(deno_last_exception(d), nullptr); + deno_delete(d); +} + +TEST(LibDenoTest, LastException) { + Deno* d = deno_new(deno_config{0, empty, empty, nullptr}); + EXPECT_EQ(deno_last_exception(d), nullptr); + deno_execute(d, nullptr, "a.js", "\n\nthrow Error('boo');\n\n"); + EXPECT_STREQ(deno_last_exception(d), + "{\"message\":\"Uncaught Error: boo\",\"sourceLine\":\"throw " + "Error('boo');\",\"scriptResourceName\":\"a.js\",\"lineNumber\":" + "3,\"startPosition\":8,\"endPosition\":9,\"errorLevel\":8," + "\"startColumn\":6,\"endColumn\":7,\"isSharedCrossOrigin\":" + "false,\"isOpaque\":false,\"frames\":[{\"line\":3,\"column\":7," + "\"functionName\":\"\",\"scriptName\":\"a.js\",\"isEval\":false," + "\"isConstructor\":false,\"isWasm\":false}]}"); + deno_delete(d); +} + +TEST(LibDenoTest, EncodeErrorBug) { + Deno* d = deno_new(deno_config{0, empty, empty, nullptr}); + EXPECT_EQ(deno_last_exception(d), nullptr); + deno_execute(d, nullptr, "a.js", "eval('a')"); + EXPECT_STREQ( + deno_last_exception(d), + "{\"message\":\"Uncaught ReferenceError: a is not " + "defined\",\"sourceLine\":\"a\",\"lineNumber\":1,\"startPosition\":0," + "\"endPosition\":1,\"errorLevel\":8,\"startColumn\":0,\"endColumn\":1," + "\"isSharedCrossOrigin\":false,\"isOpaque\":false,\"frames\":[{\"line\":" + "1,\"column\":1,\"functionName\":\"\",\"scriptName\":\"\"," + "\"isEval\":true,\"isConstructor\":false,\"isWasm\":false},{\"line\":1," + "\"column\":1,\"functionName\":\"\",\"scriptName\":\"a.js\",\"isEval\":" + "false,\"isConstructor\":false,\"isWasm\":false}]}"); + deno_delete(d); +} + +TEST(LibDenoTest, Shared) { + uint8_t s[] = {0, 1, 2}; + deno_buf shared = {nullptr, 0, s, 3, 0}; + Deno* d = deno_new(deno_config{0, snapshot, shared, nullptr}); + deno_execute(d, nullptr, "a.js", "Shared()"); + EXPECT_EQ(nullptr, deno_last_exception(d)); + EXPECT_EQ(s[0], 42); + EXPECT_EQ(s[1], 43); + EXPECT_EQ(s[2], 44); + deno_delete(d); +} + +TEST(LibDenoTest, Utf8Bug) { + Deno* d = deno_new(deno_config{0, empty, empty, nullptr}); + // The following is a valid UTF-8 javascript which just defines a string + // literal. We had a bug where libdeno would choke on this. + deno_execute(d, nullptr, "a.js", "x = \"\xEF\xBF\xBD\""); + EXPECT_EQ(nullptr, deno_last_exception(d)); + deno_delete(d); +} + +TEST(LibDenoTest, LibDenoEvalContext) { + Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); + deno_execute(d, nullptr, "a.js", "LibDenoEvalContext();"); + EXPECT_EQ(nullptr, deno_last_exception(d)); + deno_delete(d); +} + +TEST(LibDenoTest, LibDenoEvalContextError) { + Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); + deno_execute(d, nullptr, "a.js", "LibDenoEvalContextError();"); + EXPECT_EQ(nullptr, deno_last_exception(d)); + deno_delete(d); +} + +TEST(LibDenoTest, SharedAtomics) { + int32_t s[] = {0, 1, 2}; + deno_buf shared = {nullptr, 0, reinterpret_cast(s), sizeof s, 0}; + Deno* d = deno_new(deno_config{0, empty, shared, nullptr}); + deno_execute(d, nullptr, "a.js", + "Atomics.add(new Int32Array(Deno.core.shared), 0, 1)"); + EXPECT_EQ(nullptr, deno_last_exception(d)); + EXPECT_EQ(s[0], 1); + EXPECT_EQ(s[1], 1); + EXPECT_EQ(s[2], 2); + deno_delete(d); +} diff --git a/core/libdeno/libdeno_test.js b/core/libdeno/libdeno_test.js new file mode 100644 index 000000000..156af1e47 --- /dev/null +++ b/core/libdeno/libdeno_test.js @@ -0,0 +1,197 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. + +// A simple runtime that doesn't involve typescript or protobufs to test +// libdeno. Invoked by libdeno_test.cc + +const global = this; + +function assert(cond) { + if (!cond) throw Error("libdeno_test.js assert failed"); +} + +global.CanCallFunction = () => { + Deno.core.print("Hello world from foo"); + return "foo"; +}; + +// This object is created to test snapshotting. +// See DeserializeInternalFieldsCallback and SerializeInternalFieldsCallback. +const snapshotted = new Uint8Array([1, 3, 3, 7]); + +global.TypedArraySnapshots = () => { + assert(snapshotted[0] === 1); + assert(snapshotted[1] === 3); + assert(snapshotted[2] === 3); + assert(snapshotted[3] === 7); +}; + +global.RecvReturnEmpty = () => { + const m1 = new Uint8Array("abc".split("").map(c => c.charCodeAt(0))); + const m2 = m1.slice(); + const r1 = Deno.core.send(m1); + assert(r1 == null); + const r2 = Deno.core.send(m2); + assert(r2 == null); +}; + +global.RecvReturnBar = () => { + const m = new Uint8Array("abc".split("").map(c => c.charCodeAt(0))); + const r = Deno.core.send(m); + assert(r instanceof Uint8Array); + assert(r.byteLength === 3); + const rstr = String.fromCharCode(...r); + assert(rstr === "bar"); +}; + +global.DoubleRecvFails = () => { + // Deno.core.recv is an internal function and should only be called once from the + // runtime. + Deno.core.recv((_channel, _msg) => assert(false)); + Deno.core.recv((_channel, _msg) => assert(false)); +}; + +global.SendRecvSlice = () => { + const abLen = 1024; + let buf = new Uint8Array(abLen); + for (let i = 0; i < 5; i++) { + // Set first and last byte, for verification by the native side. + buf[0] = 100 + i; + buf[buf.length - 1] = 100 - i; + // On the native side, the slice is shortened by 19 bytes. + buf = Deno.core.send(buf); + assert(buf.byteOffset === i * 11); + assert(buf.byteLength === abLen - i * 30 - 19); + assert(buf.buffer.byteLength == abLen); + // Look for values written by the backend. + assert(buf[0] === 200 + i); + assert(buf[buf.length - 1] === 200 - i); + // On the JS side, the start of the slice is moved up by 11 bytes. + buf = buf.subarray(11); + assert(buf.byteOffset === (i + 1) * 11); + assert(buf.byteLength === abLen - (i + 1) * 30); + } +}; + +global.JSSendArrayBufferViewTypes = () => { + // Test that ArrayBufferView slices are transferred correctly. + // Send Uint8Array. + const ab1 = new ArrayBuffer(4321); + const u8 = new Uint8Array(ab1, 2468, 1000); + u8[0] = 1; + Deno.core.send(u8); + // Send Uint32Array. + const ab2 = new ArrayBuffer(4321); + const u32 = new Uint32Array(ab2, 2468, 1000 / Uint32Array.BYTES_PER_ELEMENT); + u32[0] = 0x02020202; + Deno.core.send(u32); + // Send DataView. + const ab3 = new ArrayBuffer(4321); + const dv = new DataView(ab3, 2468, 1000); + dv.setUint8(0, 3); + Deno.core.send(dv); +}; + +// The following join has caused SnapshotBug to segfault when using kKeep. +[].join(""); + +global.SnapshotBug = () => { + assert("1,2,3" === String([1, 2, 3])); +}; + +global.GlobalErrorHandling = () => { + eval("\n\n notdefined()\n//# sourceURL=helloworld.js"); +}; + +// Allocate this buf at the top level to avoid GC. +const zeroCopyBuf = new Uint8Array([3, 4]); + +global.ZeroCopyBuf = () => { + const a = new Uint8Array([1, 2]); + const b = zeroCopyBuf; + // The second parameter of send should modified by the + // privileged side. + const r = Deno.core.send(a, b); + assert(r == null); + // b is different. + assert(b[0] === 4); + assert(b[1] === 2); + // Now we modify it again. + b[0] = 9; + b[1] = 8; +}; + +global.CheckPromiseErrors = () => { + async function fn() { + throw new Error("message"); + } + + (async () => { + try { + await fn(); + } catch (e) { + Deno.core.send(new Uint8Array([42])); + } + })(); +}; + +global.Shared = () => { + const ab = Deno.core.shared; + assert(ab instanceof SharedArrayBuffer); + assert(Deno.core.shared != undefined); + assert(ab.byteLength === 3); + const ui8 = new Uint8Array(ab); + assert(ui8[0] === 0); + assert(ui8[1] === 1); + assert(ui8[2] === 2); + ui8[0] = 42; + ui8[1] = 43; + ui8[2] = 44; +}; + +global.LibDenoEvalContext = () => { + const [result, errInfo] = Deno.core.evalContext("let a = 1; a"); + assert(result === 1); + assert(!errInfo); + const [result2, errInfo2] = Deno.core.evalContext("a = a + 1; a"); + assert(result2 === 2); + assert(!errInfo2); +}; + +global.LibDenoEvalContextError = () => { + const [result, errInfo] = Deno.core.evalContext("not_a_variable"); + assert(!result); + assert(!!errInfo); + assert(errInfo.isNativeError); // is a native error (ReferenceError) + assert(!errInfo.isCompileError); // is NOT a compilation error + assert(errInfo.thrown.message === "not_a_variable is not defined"); + + const [result2, errInfo2] = Deno.core.evalContext("throw 1"); + assert(!result2); + assert(!!errInfo2); + assert(!errInfo2.isNativeError); // is NOT a native error + assert(!errInfo2.isCompileError); // is NOT a compilation error + assert(errInfo2.thrown === 1); + + const [result3, errInfo3] = Deno.core.evalContext( + "class AError extends Error {}; throw new AError('e')" + ); + assert(!result3); + assert(!!errInfo3); + assert(errInfo3.isNativeError); // extend from native error, still native error + assert(!errInfo3.isCompileError); // is NOT a compilation error + assert(errInfo3.thrown.message === "e"); + + const [result4, errInfo4] = Deno.core.evalContext("{"); + assert(!result4); + assert(!!errInfo4); + assert(errInfo4.isNativeError); // is a native error (SyntaxError) + assert(errInfo4.isCompileError); // is a compilation error! (braces not closed) + assert(errInfo4.thrown.message === "Unexpected end of input"); + + const [result5, errInfo5] = Deno.core.evalContext("eval('{')"); + assert(!result5); + assert(!!errInfo5); + assert(errInfo5.isNativeError); // is a native error (SyntaxError) + assert(!errInfo5.isCompileError); // is NOT a compilation error! (just eval) + assert(errInfo5.thrown.message === "Unexpected end of input"); +}; diff --git a/core/libdeno/modules.cc b/core/libdeno/modules.cc new file mode 100644 index 000000000..0b408aec8 --- /dev/null +++ b/core/libdeno/modules.cc @@ -0,0 +1,154 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. +#include "exceptions.h" +#include "internal.h" + +using deno::DenoIsolate; +using deno::HandleException; +using v8::Boolean; +using v8::Context; +using v8::EscapableHandleScope; +using v8::HandleScope; +using v8::Integer; +using v8::Isolate; +using v8::Local; +using v8::Locker; +using v8::Module; +using v8::Object; +using v8::ScriptCompiler; +using v8::ScriptOrigin; +using v8::String; +using v8::Value; + +v8::MaybeLocal ResolveCallback(Local context, + Local specifier, + Local referrer) { + auto* isolate = context->GetIsolate(); + v8::Isolate::Scope isolate_scope(isolate); + v8::Locker locker(isolate); + + DenoIsolate* d = DenoIsolate::FromIsolate(isolate); + + v8::EscapableHandleScope handle_scope(isolate); + + deno_mod referrer_id = referrer->GetIdentityHash(); + auto* referrer_info = d->GetModuleInfo(referrer_id); + CHECK_NOT_NULL(referrer_info); + + for (int i = 0; i < referrer->GetModuleRequestsLength(); i++) { + Local req = referrer->GetModuleRequest(i); + + if (req->Equals(context, specifier).ToChecked()) { + v8::String::Utf8Value req_utf8(isolate, req); + std::string req_str(*req_utf8); + + 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. + auto* info = d->GetModuleInfo(id); + if (info == nullptr) { + char buf[64 * 1024]; + snprintf(buf, sizeof(buf), "Cannot resolve module \"%s\" from \"%s\"", + req_str.c_str(), referrer_info->name.c_str()); + isolate->ThrowException(deno::v8_str(buf)); + break; + } else { + Local child_mod = info->handle.Get(isolate); + return handle_scope.Escape(child_mod); + } + } + } + + return v8::MaybeLocal(); // Error +} + +extern "C" { + +deno_mod deno_mod_new(Deno* d_, bool main, const char* name_cstr, + const char* source_cstr) { + auto* d = unwrap(d_); + return d->RegisterModule(main, name_cstr, source_cstr); +} + +const char* deno_mod_name(Deno* d_, deno_mod id) { + auto* d = unwrap(d_); + auto* info = d->GetModuleInfo(id); + return info->name.c_str(); +} + +size_t deno_mod_imports_len(Deno* d_, deno_mod id) { + auto* d = unwrap(d_); + auto* info = d->GetModuleInfo(id); + return info->import_specifiers.size(); +} + +const char* deno_mod_imports_get(Deno* d_, deno_mod id, size_t index) { + auto* d = unwrap(d_); + auto* info = d->GetModuleInfo(id); + if (info == nullptr || index >= info->import_specifiers.size()) { + return nullptr; + } else { + return info->import_specifiers[index].c_str(); + } +} + +void deno_mod_instantiate(Deno* d_, void* user_data, deno_mod id, + deno_resolve_cb cb) { + auto* d = unwrap(d_); + deno::UserDataScope user_data_scope(d, user_data); + + auto* isolate = d->isolate_; + v8::Isolate::Scope isolate_scope(isolate); + v8::Locker locker(isolate); + v8::HandleScope handle_scope(isolate); + auto context = d->context_.Get(d->isolate_); + v8::Context::Scope context_scope(context); + + v8::TryCatch try_catch(isolate); + { + CHECK_NULL(d->resolve_cb_); + d->resolve_cb_ = cb; + { + auto* info = d->GetModuleInfo(id); + if (info == nullptr) { + return; + } + Local module = info->handle.Get(isolate); + if (module->GetStatus() == Module::kErrored) { + return; + } + auto maybe_ok = module->InstantiateModule(context, ResolveCallback); + CHECK(maybe_ok.IsJust() || try_catch.HasCaught()); + } + d->resolve_cb_ = nullptr; + } + + if (try_catch.HasCaught()) { + HandleException(context, try_catch.Exception()); + } +} + +void deno_mod_evaluate(Deno* d_, void* user_data, deno_mod id) { + auto* d = unwrap(d_); + deno::UserDataScope user_data_scope(d, user_data); + + auto* isolate = d->isolate_; + v8::Isolate::Scope isolate_scope(isolate); + v8::Locker locker(isolate); + v8::HandleScope handle_scope(isolate); + auto context = d->context_.Get(d->isolate_); + v8::Context::Scope context_scope(context); + + auto* info = d->GetModuleInfo(id); + Local module = info->handle.Get(isolate); + + CHECK_EQ(Module::kInstantiated, module->GetStatus()); + + auto maybe_result = module->Evaluate(context); + if (maybe_result.IsEmpty()) { + CHECK_EQ(Module::kErrored, module->GetStatus()); + HandleException(context, module->GetException()); + } +} + +} // extern "C" diff --git a/core/libdeno/modules_test.cc b/core/libdeno/modules_test.cc new file mode 100644 index 000000000..9f9228430 --- /dev/null +++ b/core/libdeno/modules_test.cc @@ -0,0 +1,148 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. +#include "test.h" + +static int exec_count = 0; +void recv_cb(void* user_data, deno_buf buf, deno_buf zero_copy_buf) { + // We use this to check that scripts have executed. + EXPECT_EQ(1u, buf.data_len); + EXPECT_EQ(buf.data_ptr[0], 4); + EXPECT_EQ(zero_copy_buf.zero_copy_id, 0u); + EXPECT_EQ(zero_copy_buf.data_ptr, nullptr); + exec_count++; +} + +TEST(ModulesTest, Resolution) { + exec_count = 0; // Reset + Deno* d = deno_new(deno_config{0, empty, empty, recv_cb}); + EXPECT_EQ(0, exec_count); + + static deno_mod a = deno_mod_new(d, true, "a.js", + "import { b } from 'b.js'\n" + "if (b() != 'b') throw Error();\n" + "Deno.core.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(1u, 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_EQ(nullptr, deno_mod_imports_get(d, a, 1)); + 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, ResolutionError) { + exec_count = 0; // Reset + Deno* d = deno_new(deno_config{0, empty, empty, recv_cb}); + EXPECT_EQ(0, exec_count); + + static deno_mod a = deno_mod_new(d, true, "a.js", + "import 'bad'\n" + "Deno.core.send(new Uint8Array([4]));"); + EXPECT_NE(a, 0); + EXPECT_EQ(nullptr, deno_last_exception(d)); + + EXPECT_EQ(0, exec_count); + + EXPECT_EQ(1u, deno_mod_imports_len(d, a)); + EXPECT_STREQ("bad", deno_mod_imports_get(d, a, 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, "bad"); + resolve_count++; + return 0; + }; + + deno_mod_instantiate(d, d, a, resolve_cb); + EXPECT_NE(nullptr, deno_last_exception(d)); + EXPECT_EQ(1, resolve_count); + EXPECT_EQ(0, exec_count); + + deno_delete(d); +} + +TEST(ModulesTest, ImportMetaUrl) { + exec_count = 0; // Reset + Deno* d = deno_new(deno_config{0, empty, empty, recv_cb}); + EXPECT_EQ(0, exec_count); + + static deno_mod a = + deno_mod_new(d, true, "a.js", + "if ('a.js' != import.meta.url) throw 'hmm'\n" + "Deno.core.send(new Uint8Array([4]));"); + EXPECT_NE(a, 0); + EXPECT_EQ(nullptr, deno_last_exception(d)); + + 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); +} + +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); +} diff --git a/core/libdeno/snapshot_creator.cc b/core/libdeno/snapshot_creator.cc new file mode 100644 index 000000000..19098392d --- /dev/null +++ b/core/libdeno/snapshot_creator.cc @@ -0,0 +1,47 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +// Hint: --trace_serializer is a useful debugging flag. +#include +#include +#include "deno.h" +#include "file_util.h" +#include "internal.h" +#include "third_party/v8/include/v8.h" +#include "third_party/v8/src/base/logging.h" + +namespace deno {} // namespace deno + +int main(int argc, char** argv) { + const char* snapshot_out_bin = argv[1]; + const char* js_fn = argv[2]; + + deno_set_v8_flags(&argc, argv); + + CHECK_NOT_NULL(js_fn); + CHECK_NOT_NULL(snapshot_out_bin); + + std::string js_source; + CHECK(deno::ReadFileToString(js_fn, &js_source)); + + deno_init(); + deno_config config = {1, deno::empty_buf, deno::empty_buf, nullptr}; + Deno* d = deno_new(config); + + deno_execute(d, nullptr, js_fn, js_source.c_str()); + if (deno_last_exception(d) != nullptr) { + std::cerr << "Snapshot Exception " << std::endl; + std::cerr << deno_last_exception(d) << std::endl; + deno_delete(d); + return 1; + } + + auto snapshot = deno_get_snapshot(d); + + std::ofstream file_(snapshot_out_bin, std::ios::binary); + file_.write(reinterpret_cast(snapshot.data_ptr), snapshot.data_len); + file_.close(); + + delete[] snapshot.data_ptr; + deno_delete(d); + + return file_.bad(); +} diff --git a/core/libdeno/test.cc b/core/libdeno/test.cc new file mode 100644 index 000000000..1340fe8c3 --- /dev/null +++ b/core/libdeno/test.cc @@ -0,0 +1,31 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +#include "test.h" +#include +#include "file_util.h" + +deno_buf snapshot = {nullptr, 0, nullptr, 0, 0}; + +int main(int argc, char** argv) { + // Locate the snapshot. + std::string exe_path; + if (!deno::ExePath(&exe_path)) { + std::cerr << "deno::ExePath() failed" << std::endl; + return 1; + } + std::string snapshot_path = deno::Dirname(exe_path) + SNAPSHOT_PATH; + + // Load the snapshot. + std::string contents; + if (!deno::ReadFileToString(snapshot_path.c_str(), &contents)) { + std::cerr << "Failed to read snapshot from " << snapshot_path << std::endl; + return 1; + } + snapshot.data_ptr = + reinterpret_cast(const_cast(contents.c_str())); + snapshot.data_len = contents.size(); + + testing::InitGoogleTest(&argc, argv); + deno_init(); + deno_set_v8_flags(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/core/libdeno/test.h b/core/libdeno/test.h new file mode 100644 index 000000000..2f7c32384 --- /dev/null +++ b/core/libdeno/test.h @@ -0,0 +1,11 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +#ifndef TEST_H_ +#define TEST_H_ + +#include "deno.h" +#include "testing/gtest/include/gtest/gtest.h" + +extern deno_buf snapshot; // Loaded in libdeno/test.cc +const deno_buf empty = {nullptr, 0, nullptr, 0, 0}; + +#endif // TEST_H_ diff --git a/deno.gni b/deno.gni new file mode 100644 index 000000000..a260edd11 --- /dev/null +++ b/deno.gni @@ -0,0 +1,65 @@ +# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import("//build/compiled_action.gni") + +# Tempalte to generate a Rollup bundle of code. +template("bundle") { + action(target_name) { + forward_variables_from(invoker, "*") + script = "//tools/run_node.py" + outputs = [ + out_dir + out_name + ".js", + out_dir + out_name + ".js.map", + ] + inputs = [ + "//js/" + out_name + ".ts", + "//rollup.config.js", + ] + depfile = out_dir + out_name + ".d" + args = [ + rebase_path("//third_party/node_modules/rollup/bin/rollup", root_build_dir), + "-c", + rebase_path("//rollup.config.js", root_build_dir), + "-i", + rebase_path(inputs[0], root_build_dir), + "-o", + rebase_path(outputs[0], root_build_dir), + "--sourcemapFile", + rebase_path("."), + "--silent", + ] + } +} + +template("run_node") { + action(target_name) { + forward_variables_from(invoker, "*") + script = "//tools/run_node.py" + } +} + +# Template to generate different V8 snapshots based on different runtime flags. +template("snapshot") { + compiled_action(target_name) { + forward_variables_from(invoker, + [ + "testonly", + "deps", + ]) + # TODO(ry) Rewrite snapshot_creator in Rust. + tool = "//core/libdeno:snapshot_creator" + visibility = [ ":*" ] # Only targets in this file can depend on this. + snapshot_out_bin = "$target_gen_dir/$target_name.bin" + inputs = [ + invoker.source_root, + ] + + outputs = [ + snapshot_out_bin, + ] + args = rebase_path(outputs, root_build_dir) + + rebase_path(inputs, root_build_dir) + + # To debug snapshotting problems: + # args += ["--trace-serializer"] + } +} diff --git a/gn.rs b/gn.rs new file mode 100644 index 000000000..5d27b8bb0 --- /dev/null +++ b/gn.rs @@ -0,0 +1,110 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +// This is used in cli/build.rs and core/build.rs to interface with the GN build +// system (which defines the deno build). + +use std::env; +use std::path::{self, Path, PathBuf}; +use std::process::Command; + +pub struct Build { + gn_mode: String, + pub gn_out_dir: String, + pub gn_out_path: PathBuf, + pub check_only: bool, +} + +impl Build { + pub fn setup() -> Build { + let gn_mode = if cfg!(target_os = "windows") { + // On Windows, we need to link with a release build of libdeno, because + // rust always uses the release CRT. + // TODO(piscisaureus): make linking with debug libdeno possible. + String::from("release") + } else { + // Cargo sets PROFILE to either "debug" or "release", which conveniently + // matches the build modes we support. + env::var("PROFILE").unwrap() + }; + + // cd into workspace root. + assert!(env::set_current_dir("..").is_ok()); + + let cwd = env::current_dir().unwrap(); + // If not using host default target the output folder will change + // target/release will become target/$TARGET/release + // Gn should also be using this output directory as well + // most things will work with gn using the default + // output directory but some tests depend on artifacts + // being in a specific directory relative to the main build output + let gn_out_path = cwd.join(format!("target/{}", gn_mode.clone())); + let gn_out_dir = normalize_path(&gn_out_path); + + // Tell Cargo when to re-run this file. We do this first, so these directives + // can take effect even if something goes wrong later in the build process. + println!("cargo:rerun-if-env-changed=DENO_BUILD_PATH"); + // TODO: this is obviously not appropriate here. + println!("cargo:rerun-if-env-changed=APPVEYOR_REPO_COMMIT"); + + // This helps Rust source files locate the snapshot, source map etc. + println!("cargo:rustc-env=GN_OUT_DIR={}", gn_out_dir); + + // Detect if we're being invoked by the rust language server (RLS). + // Unfortunately we can't detect whether we're being run by `cargo check`. + let check_only = env::var_os("CARGO") + .map(PathBuf::from) + .as_ref() + .and_then(|p| p.file_stem()) + .and_then(|f| f.to_str()) + .map(|s| s.starts_with("rls")) + .unwrap_or(false); + + if check_only { + // Enable the 'check_only' feature, which enables some workarounds in the + // rust source code to compile successfully without a bundle and snapshot + println!("cargo:rustc-cfg=feature=\"check-only\""); + } + + Build { + gn_out_dir, + gn_out_path, + check_only, + gn_mode, + } + } + + pub fn run(&self, gn_target: &str) { + if !self.gn_out_path.join("build.ninja").exists() { + let status = Command::new("python") + .env("DENO_BUILD_PATH", &self.gn_out_dir) + .env("DENO_BUILD_MODE", &self.gn_mode) + .arg("./tools/setup.py") + .status() + .expect("setup.py failed"); + assert!(status.success()); + } + + // TODO(ry) call ninja directly here, not python. + let status = Command::new("python") + .env("DENO_BUILD_PATH", &self.gn_out_dir) + .env("DENO_BUILD_MODE", &self.gn_mode) + .arg("./tools/build.py") + .arg(gn_target) + .arg("-v") + .status() + .expect("build.py failed"); + assert!(status.success()); + } +} + +// Utility function to make a path absolute, normalizing it to use forward +// slashes only. The returned value is an owned String, otherwise panics. +fn normalize_path>(path: T) -> String { + path + .as_ref() + .to_str() + .unwrap() + .to_owned() + .chars() + .map(|c| if path::is_separator(c) { '/' } else { c }) + .collect() +} diff --git a/js/assets.ts b/js/assets.ts index 5f1790646..6e576c2d1 100644 --- a/js/assets.ts +++ b/js/assets.ts @@ -6,7 +6,7 @@ // There is a rollup plugin that will inline any module ending with `!string` // Generated default library -import libDts from "gen/lib/lib.deno_runtime.d.ts!string"; +import libDts from "gen/cli/lib/lib.deno_runtime.d.ts!string"; // Static libraries import libEs2015Dts from "/third_party/node_modules/typescript/lib/lib.es2015.d.ts!string"; diff --git a/js/chmod.ts b/js/chmod.ts index 3b14ef5bc..b26f78528 100644 --- a/js/chmod.ts +++ b/js/chmod.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import * as dispatch from "./dispatch"; diff --git a/js/compiler.ts b/js/compiler.ts index 8be78935f..0f7070fd2 100644 --- a/js/compiler.ts +++ b/js/compiler.ts @@ -1,6 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import * as ts from "typescript"; -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import { window } from "./window"; import { assetSourceCode } from "./assets"; import { Console } from "./console"; diff --git a/js/copy_file.ts b/js/copy_file.ts index a7792d588..a6ff9439e 100644 --- a/js/copy_file.ts +++ b/js/copy_file.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import * as dispatch from "./dispatch"; diff --git a/js/dir.ts b/js/dir.ts index 8da72c16f..ccbbd2458 100644 --- a/js/dir.ts +++ b/js/dir.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import { assert } from "./util"; import * as flatbuffers from "./flatbuffers"; import { sendSync } from "./dispatch"; diff --git a/js/dispatch.ts b/js/dispatch.ts index 3ca0cd799..528cf320c 100644 --- a/js/dispatch.ts +++ b/js/dispatch.ts @@ -1,7 +1,7 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import { core } from "./core"; import * as flatbuffers from "./flatbuffers"; -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as errors from "./errors"; import * as util from "./util"; diff --git a/js/errors.ts b/js/errors.ts index cefa4a863..42cff60db 100644 --- a/js/errors.ts +++ b/js/errors.ts @@ -1,6 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { Base, ErrorKind } from "gen/msg_generated"; -export { ErrorKind } from "gen/msg_generated"; +import { Base, ErrorKind } from "gen/cli/msg_generated"; +export { ErrorKind } from "gen/cli/msg_generated"; /** A Deno specific error. The `kind` property is set to a specific error code * which can be used to in application logic. diff --git a/js/fetch.ts b/js/fetch.ts index 355535dfe..e4262ea0a 100644 --- a/js/fetch.ts +++ b/js/fetch.ts @@ -2,7 +2,7 @@ import { assert, createResolvable, notImplemented, isTypedArray } from "./util"; import * as flatbuffers from "./flatbuffers"; import { sendAsync } from "./dispatch"; -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as domTypes from "./dom_types"; import { TextDecoder, TextEncoder } from "./text_encoding"; import { DenoBlob, bytesSymbol as blobBytesSymbol } from "./blob"; diff --git a/js/file_info.ts b/js/file_info.ts index deaafe5ea..0026d22c8 100644 --- a/js/file_info.ts +++ b/js/file_info.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; /** A FileInfo describes a file and is returned by `stat`, `lstat`, * `statSync`, `lstatSync`. diff --git a/js/files.ts b/js/files.ts index 9da9fbe0a..3a0297566 100644 --- a/js/files.ts +++ b/js/files.ts @@ -11,7 +11,7 @@ import { SyncSeeker } from "./io"; import * as dispatch from "./dispatch"; -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import { assert } from "./util"; import * as flatbuffers from "./flatbuffers"; diff --git a/js/format_error.ts b/js/format_error.ts index ebc579355..7cdbd72c3 100644 --- a/js/format_error.ts +++ b/js/format_error.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import { sendSync } from "./dispatch"; import { assert } from "./util"; diff --git a/js/main.ts b/js/main.ts index 887d6b3c2..e7f7e284e 100644 --- a/js/main.ts +++ b/js/main.ts @@ -16,7 +16,7 @@ import { setLocation } from "./location"; import * as deno from "./deno"; // TODO(kitsonk) remove with `--types` below -import libDts from "gen/lib/lib.deno_runtime.d.ts!string"; +import libDts from "gen/cli/lib/lib.deno_runtime.d.ts!string"; export default function denoMain(): void { const startResMsg = os.start(); diff --git a/js/make_temp_dir.ts b/js/make_temp_dir.ts index dc04522ee..ff754a3a8 100644 --- a/js/make_temp_dir.ts +++ b/js/make_temp_dir.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import * as dispatch from "./dispatch"; import { assert } from "./util"; diff --git a/js/metrics.ts b/js/metrics.ts index c4ba14c7d..f00c1308d 100644 --- a/js/metrics.ts +++ b/js/metrics.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import { assert } from "./util"; import * as dispatch from "./dispatch"; diff --git a/js/mkdir.ts b/js/mkdir.ts index 784d891f9..36a2b6622 100644 --- a/js/mkdir.ts +++ b/js/mkdir.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import * as dispatch from "./dispatch"; diff --git a/js/net.ts b/js/net.ts index 0b56a4c30..e982d9455 100644 --- a/js/net.ts +++ b/js/net.ts @@ -1,6 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import { ReadResult, Reader, Writer, Closer } from "./io"; -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import { assert, notImplemented } from "./util"; import * as dispatch from "./dispatch"; import * as flatbuffers from "./flatbuffers"; diff --git a/js/os.ts b/js/os.ts index 0ad107bf1..3ef75c003 100644 --- a/js/os.ts +++ b/js/os.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import { core } from "./core"; import { handleAsyncMsgFromRust, sendSync } from "./dispatch"; import * as flatbuffers from "./flatbuffers"; diff --git a/js/performance.ts b/js/performance.ts index 73378b15c..b4d0fa43b 100644 --- a/js/performance.ts +++ b/js/performance.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import { sendSync } from "./dispatch"; import * as flatbuffers from "./flatbuffers"; import { assert } from "./util"; diff --git a/js/permissions.ts b/js/permissions.ts index f4fe6826f..ded8ac305 100644 --- a/js/permissions.ts +++ b/js/permissions.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import * as dispatch from "./dispatch"; import { assert } from "./util"; diff --git a/js/process.ts b/js/process.ts index 8ce83477a..7e31a1361 100644 --- a/js/process.ts +++ b/js/process.ts @@ -1,7 +1,7 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import * as dispatch from "./dispatch"; import * as flatbuffers from "./flatbuffers"; -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import { File, close } from "./files"; import { ReadCloser, WriteCloser } from "./io"; diff --git a/js/read_dir.ts b/js/read_dir.ts index 2515a61c6..9aa7c3375 100644 --- a/js/read_dir.ts +++ b/js/read_dir.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import * as dispatch from "./dispatch"; import { FileInfo, FileInfoImpl } from "./file_info"; diff --git a/js/read_link.ts b/js/read_link.ts index b63fdd841..0e1a950f3 100644 --- a/js/read_link.ts +++ b/js/read_link.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import { assert } from "./util"; import * as dispatch from "./dispatch"; diff --git a/js/remove.ts b/js/remove.ts index 1c9df6830..0bbfd913b 100644 --- a/js/remove.ts +++ b/js/remove.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import * as dispatch from "./dispatch"; diff --git a/js/rename.ts b/js/rename.ts index 9d475f7e9..2bb83e966 100644 --- a/js/rename.ts +++ b/js/rename.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import * as dispatch from "./dispatch"; diff --git a/js/repl.ts b/js/repl.ts index 068501873..350aff867 100644 --- a/js/repl.ts +++ b/js/repl.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import { assert } from "./util"; import { close } from "./files"; diff --git a/js/resources.ts b/js/resources.ts index 2cabd26dc..f78fd0360 100644 --- a/js/resources.ts +++ b/js/resources.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import { assert } from "./util"; import * as dispatch from "./dispatch"; diff --git a/js/stat.ts b/js/stat.ts index 9f99ab441..ed3e17d19 100644 --- a/js/stat.ts +++ b/js/stat.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import * as dispatch from "./dispatch"; import { assert } from "./util"; diff --git a/js/symlink.ts b/js/symlink.ts index 74c1f74fe..f260620d6 100644 --- a/js/symlink.ts +++ b/js/symlink.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import * as dispatch from "./dispatch"; import * as util from "./util"; diff --git a/js/timers.ts b/js/timers.ts index 4089d5f8b..e372f88c2 100644 --- a/js/timers.ts +++ b/js/timers.ts @@ -1,6 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import { assert } from "./util"; -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import { sendAsync, sendSync } from "./dispatch"; diff --git a/js/truncate.ts b/js/truncate.ts index 2d700ab5d..c721404a1 100644 --- a/js/truncate.ts +++ b/js/truncate.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import * as dispatch from "./dispatch"; diff --git a/js/workers.ts b/js/workers.ts index b2f4231c4..bdfbed640 100644 --- a/js/workers.ts +++ b/js/workers.ts @@ -1,6 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import * as dispatch from "./dispatch"; -import * as msg from "gen/msg_generated"; +import * as msg from "gen/cli/msg_generated"; import * as flatbuffers from "./flatbuffers"; import { assert, log } from "./util"; import { window } from "./window"; diff --git a/libdeno/BUILD.gn b/libdeno/BUILD.gn deleted file mode 100644 index 6cea30b2a..000000000 --- a/libdeno/BUILD.gn +++ /dev/null @@ -1,108 +0,0 @@ -# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import("//third_party/v8/gni/v8.gni") -import("./deno.gni") - -config("deno_config") { - include_dirs = [ "//third_party/v8" ] # This allows us to v8/src/base/ libraries. - configs = [ "//third_party/v8:external_config" ] - cflags = [] - - if (is_debug) { - defines = [ "DEBUG" ] - } - - if (is_clang) { - cflags += [ - "-fcolor-diagnostics", - "-fansi-escape-codes", - ] - if (is_debug) { - cflags += [ "-glldb" ] - } - } - - if (is_win) { - # The `/Zl` ("omit default library name") flag makes the compiler produce - # object files that can link with both the static and dynamic CRT. - cflags += [ "/Zl" ] - } -} - -v8_source_set("v8") { - deps = [ - "//third_party/v8:v8", - "//third_party/v8:v8_libbase", - "//third_party/v8:v8_libplatform", - "//third_party/v8:v8_libsampler", - ] - configs = [ ":deno_config" ] -} - -# Only functionality needed for libdeno_test and snapshot_creator -# In particular no flatbuffers, no assets, no rust, no msg handlers. -# Because snapshots are slow, it's important that snapshot_creator's -# dependencies are minimal. -v8_source_set("libdeno") { - sources = [ - "api.cc", - "binding.cc", - "deno.h", - "exceptions.cc", - "exceptions.h", - "file_util.cc", - "file_util.h", - "internal.h", - "modules.cc", - ] - deps = [ - ":v8", - ] - configs = [ ":deno_config" ] -} - -# The cargo-driven build links with libdeno to pull in all non-rust code. -v8_static_library("libdeno_static_lib") { - output_name = "libdeno" - deps = [ - ":libdeno", - "//build/config:shared_library_deps", - ] - configs = [ ":deno_config" ] -} - -v8_executable("snapshot_creator") { - sources = [ - "snapshot_creator.cc", - ] - deps = [ - ":libdeno", - ] - configs = [ ":deno_config" ] -} - -v8_executable("test_cc") { - testonly = true - sources = [ - "file_util_test.cc", - "libdeno_test.cc", - "modules_test.cc", - "test.cc", - ] - deps = [ - ":libdeno", - ":snapshot_test", - "//testing/gtest:gtest", - ] - data = [ - "$target_gen_dir/snapshot_test.bin", - ] - snapshot_path = rebase_path(data[0], root_build_dir) - defines = [ "SNAPSHOT_PATH=\"$snapshot_path\"" ] - configs = [ ":deno_config" ] -} - -# Generates $target_gen_dir/snapshot_test.bin -snapshot("snapshot_test") { - testonly = true - source_root = "libdeno_test.js" -} diff --git a/libdeno/api.cc b/libdeno/api.cc deleted file mode 100644 index fa1fe92f9..000000000 --- a/libdeno/api.cc +++ /dev/null @@ -1,231 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -#include -#include -#include -#include -#include - -#include "third_party/v8/include/libplatform/libplatform.h" -#include "third_party/v8/include/v8.h" -#include "third_party/v8/src/base/logging.h" - -#include "deno.h" -#include "exceptions.h" -#include "file_util.h" -#include "internal.h" - -extern "C" { - -Deno* deno_new_snapshotter(deno_config config) { - CHECK(config.will_snapshot); - // TODO(ry) Support loading snapshots before snapshotting. - CHECK_NULL(config.load_snapshot.data_ptr); - auto* creator = new v8::SnapshotCreator(deno::external_references); - auto* isolate = creator->GetIsolate(); - auto* d = new deno::DenoIsolate(config); - d->snapshot_creator_ = creator; - d->AddIsolate(isolate); - { - v8::Locker locker(isolate); - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope handle_scope(isolate); - auto context = v8::Context::New(isolate); - d->context_.Reset(isolate, context); - - creator->SetDefaultContext(context, - v8::SerializeInternalFieldsCallback( - deno::SerializeInternalFields, nullptr)); - deno::InitializeContext(isolate, context); - } - return reinterpret_cast(d); -} - -Deno* deno_new(deno_config config) { - if (config.will_snapshot) { - return deno_new_snapshotter(config); - } - deno::DenoIsolate* d = new deno::DenoIsolate(config); - v8::Isolate::CreateParams params; - params.array_buffer_allocator = d->array_buffer_allocator_; - params.external_references = deno::external_references; - - if (config.load_snapshot.data_ptr) { - params.snapshot_blob = &d->snapshot_; - } - - v8::Isolate* isolate = v8::Isolate::New(params); - d->AddIsolate(isolate); - - v8::Locker locker(isolate); - v8::Isolate::Scope isolate_scope(isolate); - { - v8::HandleScope handle_scope(isolate); - auto context = - v8::Context::New(isolate, nullptr, v8::MaybeLocal(), - v8::MaybeLocal(), - v8::DeserializeInternalFieldsCallback( - deno::DeserializeInternalFields, nullptr)); - if (!config.load_snapshot.data_ptr) { - // If no snapshot is provided, we initialize the context with empty - // main source code and source maps. - deno::InitializeContext(isolate, context); - } - d->context_.Reset(isolate, context); - } - - return reinterpret_cast(d); -} - -deno::DenoIsolate* unwrap(Deno* d_) { - return reinterpret_cast(d_); -} - -void deno_lock(Deno* d_) { - auto* d = unwrap(d_); - CHECK_NULL(d->locker_); - d->locker_ = new v8::Locker(d->isolate_); -} - -void deno_unlock(Deno* d_) { - auto* d = unwrap(d_); - CHECK_NOT_NULL(d->locker_); - delete d->locker_; - d->locker_ = nullptr; -} - -deno_buf deno_get_snapshot(Deno* d_) { - auto* d = unwrap(d_); - CHECK_NOT_NULL(d->snapshot_creator_); - d->ClearModules(); - d->context_.Reset(); - - auto blob = d->snapshot_creator_->CreateBlob( - v8::SnapshotCreator::FunctionCodeHandling::kKeep); - return {nullptr, 0, reinterpret_cast(const_cast(blob.data)), - blob.raw_size, 0}; -} - -static std::unique_ptr platform; - -void deno_init() { - if (platform.get() == nullptr) { - platform = v8::platform::NewDefaultPlatform(); - v8::V8::InitializePlatform(platform.get()); - v8::V8::Initialize(); - } -} - -const char* deno_v8_version() { return v8::V8::GetVersion(); } - -void deno_set_v8_flags(int* argc, char** argv) { - v8::V8::SetFlagsFromCommandLine(argc, argv, true); -} - -const char* deno_last_exception(Deno* d_) { - auto* d = unwrap(d_); - if (d->last_exception_.length() > 0) { - return d->last_exception_.c_str(); - } else { - return nullptr; - } -} - -void deno_execute(Deno* d_, void* user_data, const char* js_filename, - const char* js_source) { - auto* d = unwrap(d_); - deno::UserDataScope user_data_scope(d, user_data); - auto* isolate = d->isolate_; - v8::Locker locker(isolate); - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope handle_scope(isolate); - auto context = d->context_.Get(d->isolate_); - CHECK(!context.IsEmpty()); - deno::Execute(context, js_filename, js_source); -} - -void deno_zero_copy_release(Deno* d_, size_t zero_copy_id) { - auto* d = unwrap(d_); - v8::Isolate::Scope isolate_scope(d->isolate_); - v8::Locker locker(d->isolate_); - v8::HandleScope handle_scope(d->isolate_); - d->DeleteZeroCopyRef(zero_copy_id); -} - -void deno_respond(Deno* d_, void* user_data, deno_buf buf) { - auto* d = unwrap(d_); - if (d->current_args_ != nullptr) { - // Synchronous response. - if (buf.data_ptr != nullptr) { - DCHECK_EQ(buf.zero_copy_id, 0); - auto ab = deno::ImportBuf(d, buf); - d->current_args_->GetReturnValue().Set(ab); - } - d->current_args_ = nullptr; - return; - } - - // Asynchronous response. - deno::UserDataScope user_data_scope(d, user_data); - v8::Locker locker(d->isolate_); - v8::Isolate::Scope isolate_scope(d->isolate_); - v8::HandleScope handle_scope(d->isolate_); - - auto context = d->context_.Get(d->isolate_); - v8::Context::Scope context_scope(context); - - v8::TryCatch try_catch(d->isolate_); - - auto recv_ = d->recv_.Get(d->isolate_); - if (recv_.IsEmpty()) { - d->last_exception_ = "Deno.core.recv has not been called."; - return; - } - - v8::Local args[1]; - int argc = 0; - - // You cannot use zero_copy_buf with deno_respond(). Use - // deno_zero_copy_release() instead. - DCHECK_EQ(buf.zero_copy_id, 0); - if (buf.data_ptr != nullptr) { - args[0] = deno::ImportBuf(d, buf); - argc = 1; - } - - auto v = recv_->Call(context, context->Global(), argc, args); - - if (try_catch.HasCaught()) { - CHECK(v.IsEmpty()); - deno::HandleException(context, try_catch.Exception()); - } -} - -void deno_check_promise_errors(Deno* d_) { - auto* d = unwrap(d_); - if (d->pending_promise_map_.size() > 0) { - auto* isolate = d->isolate_; - v8::Locker locker(isolate); - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope handle_scope(isolate); - auto context = d->context_.Get(d->isolate_); - v8::Context::Scope context_scope(context); - - auto it = d->pending_promise_map_.begin(); - while (it != d->pending_promise_map_.end()) { - auto error = it->second.Get(isolate); - deno::HandleException(context, error); - it = d->pending_promise_map_.erase(it); - } - } -} - -void deno_delete(Deno* d_) { - deno::DenoIsolate* d = reinterpret_cast(d_); - delete d; -} - -void deno_terminate_execution(Deno* d_) { - deno::DenoIsolate* d = reinterpret_cast(d_); - d->isolate_->TerminateExecution(); -} -} diff --git a/libdeno/binding.cc b/libdeno/binding.cc deleted file mode 100644 index ab633f46d..000000000 --- a/libdeno/binding.cc +++ /dev/null @@ -1,563 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -#include -#include -#include -#include -#include -#include - -#ifdef _WIN32 -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif -#include -#include -#endif // _WIN32 - -#include "third_party/v8/include/v8.h" -#include "third_party/v8/src/base/logging.h" - -#include "deno.h" -#include "exceptions.h" -#include "internal.h" - -#define GLOBAL_IMPORT_BUF_SIZE 1024 - -namespace deno { - -std::vector deserialized_data; - -void DeserializeInternalFields(v8::Local holder, int index, - v8::StartupData payload, void* data) { - DCHECK_NULL(data); - if (payload.raw_size == 0) { - holder->SetAlignedPointerInInternalField(index, nullptr); - return; - } - InternalFieldData* embedder_field = new InternalFieldData{0}; - memcpy(embedder_field, payload.data, payload.raw_size); - holder->SetAlignedPointerInInternalField(index, embedder_field); - deserialized_data.push_back(embedder_field); -} - -v8::StartupData SerializeInternalFields(v8::Local holder, int index, - void* data) { - DCHECK_NULL(data); - InternalFieldData* embedder_field = static_cast( - holder->GetAlignedPointerFromInternalField(index)); - if (embedder_field == nullptr) return {nullptr, 0}; - int size = sizeof(*embedder_field); - char* payload = new char[size]; - // We simply use memcpy to serialize the content. - memcpy(payload, embedder_field, size); - return {payload, size}; -} - -// Extracts a C string from a v8::V8 Utf8Value. -const char* ToCString(const v8::String::Utf8Value& value) { - return *value ? *value : ""; -} - -void PromiseRejectCallback(v8::PromiseRejectMessage promise_reject_message) { - auto* isolate = v8::Isolate::GetCurrent(); - DenoIsolate* d = static_cast(isolate->GetData(0)); - DCHECK_EQ(d->isolate_, isolate); - v8::HandleScope handle_scope(d->isolate_); - auto error = promise_reject_message.GetValue(); - auto context = d->context_.Get(d->isolate_); - auto promise = promise_reject_message.GetPromise(); - - v8::Context::Scope context_scope(context); - - int promise_id = promise->GetIdentityHash(); - switch (promise_reject_message.GetEvent()) { - case v8::kPromiseRejectWithNoHandler: - // Insert the error into the pending_promise_map_ using the promise's id - // as the key. - d->pending_promise_map_.emplace(std::piecewise_construct, - std::make_tuple(promise_id), - std::make_tuple(d->isolate_, error)); - break; - - case v8::kPromiseHandlerAddedAfterReject: - d->pending_promise_map_.erase(promise_id); - break; - - case v8::kPromiseRejectAfterResolved: - break; - - case v8::kPromiseResolveAfterResolved: - // Should not warn. See #1272 - break; - - default: - CHECK(false && "unreachable"); - } -} - -void Print(const v8::FunctionCallbackInfo& args) { - CHECK_GE(args.Length(), 1); - CHECK_LE(args.Length(), 3); - auto* isolate = args.GetIsolate(); - DenoIsolate* d = DenoIsolate::FromIsolate(isolate); - auto context = d->context_.Get(d->isolate_); - v8::HandleScope handle_scope(isolate); - bool is_err = - args.Length() >= 2 ? args[1]->BooleanValue(context).ToChecked() : false; - FILE* file = is_err ? stderr : stdout; - -#ifdef _WIN32 - int fd = _fileno(file); - if (fd < 0) return; - - HANDLE h = reinterpret_cast(_get_osfhandle(fd)); - if (h == INVALID_HANDLE_VALUE) return; - - DWORD mode; - if (GetConsoleMode(h, &mode)) { - // Print to Windows console. Since the Windows API generally doesn't support - // UTF-8 encoded text, we have to use `WriteConsoleW()` which uses UTF-16. - v8::String::Value str(isolate, args[0]); - auto str_len = static_cast(str.length()); - auto str_wchars = reinterpret_cast(*str); - - // WriteConsoleW has some limit to how many characters can be written at - // once, which is unspecified but low enough to be encountered in practice. - // Therefore we break up the write into chunks of 8kb if necessary. - size_t chunk_start = 0; - while (chunk_start < str_len) { - size_t chunk_end = std::min(chunk_start + 8192, str_len); - - // Do not break in the middle of a surrogate pair. Note that `chunk_end` - // points to the start of the next chunk, so we check whether it contains - // the second half of a surrogate pair (a.k.a. "low surrogate"). - if (chunk_end < str_len && str_wchars[chunk_end] >= 0xdc00 && - str_wchars[chunk_end] <= 0xdfff) { - --chunk_end; - } - - // Write to the console. - DWORD chunk_len = static_cast(chunk_end - chunk_start); - DWORD _; - WriteConsoleW(h, &str_wchars[chunk_start], chunk_len, &_, nullptr); - - chunk_start = chunk_end; - } - return; - } -#endif // _WIN32 - - v8::String::Utf8Value str(isolate, args[0]); - fwrite(*str, sizeof(**str), str.length(), file); - fflush(file); -} - -void ErrorToJSON(const v8::FunctionCallbackInfo& args) { - CHECK_EQ(args.Length(), 1); - auto* isolate = args.GetIsolate(); - DenoIsolate* d = DenoIsolate::FromIsolate(isolate); - auto context = d->context_.Get(d->isolate_); - v8::HandleScope handle_scope(isolate); - auto json_string = EncodeExceptionAsJSON(context, args[0]); - args.GetReturnValue().Set(v8_str(json_string.c_str())); -} - -v8::Local ImportBuf(DenoIsolate* d, deno_buf buf) { - // Do not use ImportBuf with zero_copy buffers. - DCHECK_EQ(buf.zero_copy_id, 0); - - if (buf.data_ptr == nullptr) { - return v8::Local(); - } - - if (buf.alloc_ptr == nullptr) { - // If alloc_ptr isn't set, we memcpy. - // This is currently used for flatbuffers created in Rust. - - // To avoid excessively allocating new ArrayBuffers, we try to reuse a - // single global ArrayBuffer. The caveat is that users must extract data - // from it before the next tick. We only do this for ArrayBuffers less than - // 1024 bytes. - v8::Local ab; - void* data; - if (buf.data_len > GLOBAL_IMPORT_BUF_SIZE) { - // Simple case. We allocate a new ArrayBuffer for this. - ab = v8::ArrayBuffer::New(d->isolate_, buf.data_len); - data = ab->GetContents().Data(); - } else { - // Fast case. We reuse the global ArrayBuffer. - if (d->global_import_buf_.IsEmpty()) { - // Lazily initialize it. - DCHECK_NULL(d->global_import_buf_ptr_); - ab = v8::ArrayBuffer::New(d->isolate_, GLOBAL_IMPORT_BUF_SIZE); - d->global_import_buf_.Reset(d->isolate_, ab); - d->global_import_buf_ptr_ = ab->GetContents().Data(); - } else { - DCHECK(d->global_import_buf_ptr_); - ab = d->global_import_buf_.Get(d->isolate_); - } - data = d->global_import_buf_ptr_; - } - memcpy(data, buf.data_ptr, buf.data_len); - auto view = v8::Uint8Array::New(ab, 0, buf.data_len); - return view; - } else { - auto ab = v8::ArrayBuffer::New( - d->isolate_, reinterpret_cast(buf.alloc_ptr), buf.alloc_len, - v8::ArrayBufferCreationMode::kInternalized); - auto view = - v8::Uint8Array::New(ab, buf.data_ptr - buf.alloc_ptr, buf.data_len); - return view; - } -} - -static deno_buf GetContents(v8::Isolate* isolate, - v8::Local view) { - auto ab = view->Buffer(); - auto contents = ab->GetContents(); - deno_buf buf; - buf.alloc_ptr = reinterpret_cast(contents.Data()); - buf.alloc_len = contents.ByteLength(); - buf.data_ptr = buf.alloc_ptr + view->ByteOffset(); - buf.data_len = view->ByteLength(); - return buf; -} - -// Sets the recv_ callback. -void Recv(const v8::FunctionCallbackInfo& args) { - v8::Isolate* isolate = args.GetIsolate(); - DenoIsolate* d = DenoIsolate::FromIsolate(isolate); - DCHECK_EQ(d->isolate_, isolate); - - v8::HandleScope handle_scope(isolate); - - if (!d->recv_.IsEmpty()) { - isolate->ThrowException(v8_str("Deno.core.recv already called.")); - return; - } - - v8::Local v = args[0]; - CHECK(v->IsFunction()); - v8::Local func = v8::Local::Cast(v); - - d->recv_.Reset(isolate, func); -} - -void Send(const v8::FunctionCallbackInfo& args) { - v8::Isolate* isolate = args.GetIsolate(); - DenoIsolate* d = DenoIsolate::FromIsolate(isolate); - DCHECK_EQ(d->isolate_, isolate); - - deno_buf control = {nullptr, 0u, nullptr, 0u, 0u}; - deno_buf zero_copy = {nullptr, 0u, nullptr, 0u, 0u}; - - v8::HandleScope handle_scope(isolate); - - if (args.Length() > 0) { - v8::Local control_v = args[0]; - if (control_v->IsArrayBufferView()) { - control = - GetContents(isolate, v8::Local::Cast(control_v)); - } - } - - v8::Local zero_copy_v; - if (args.Length() == 2) { - if (args[1]->IsArrayBufferView()) { - zero_copy_v = args[1]; - zero_copy = GetContents( - isolate, v8::Local::Cast(zero_copy_v)); - size_t zero_copy_id = d->next_zero_copy_id_++; - DCHECK_GT(zero_copy_id, 0); - zero_copy.zero_copy_id = zero_copy_id; - // If the zero_copy ArrayBuffer was given, we must maintain a strong - // reference to it until deno_zero_copy_release is called. - d->AddZeroCopyRef(zero_copy_id, zero_copy_v); - } - } - - DCHECK_NULL(d->current_args_); - d->current_args_ = &args; - - d->recv_cb_(d->user_data_, control, zero_copy); - - if (d->current_args_ == nullptr) { - // This indicates that deno_repond() was called already. - } else { - // Asynchronous. - d->current_args_ = nullptr; - } -} - -v8::ScriptOrigin ModuleOrigin(v8::Isolate* isolate, - v8::Local resource_name) { - return v8::ScriptOrigin(resource_name, v8::Local(), - v8::Local(), v8::Local(), - v8::Local(), v8::Local(), - v8::Local(), v8::Local(), - v8::True(isolate)); -} - -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_); - auto context = context_.Get(isolate_); - v8::Context::Scope context_scope(context); - - v8::Local name_str = v8_str(name); - v8::Local source_str = v8_str(source); - - auto origin = ModuleOrigin(isolate_, name_str); - v8::ScriptCompiler::Source source_(source_str, origin); - - v8::TryCatch try_catch(isolate_); - - auto maybe_module = v8::ScriptCompiler::CompileModule(isolate_, &source_); - - if (try_catch.HasCaught()) { - CHECK(maybe_module.IsEmpty()); - HandleException(context, try_catch.Exception()); - return 0; - } - - auto module = maybe_module.ToLocalChecked(); - - int id = module->GetIdentityHash(); - - std::vector import_specifiers; - - for (int i = 0; i < module->GetModuleRequestsLength(); ++i) { - v8::Local specifier = module->GetModuleRequest(i); - v8::String::Utf8Value specifier_utf8(isolate_, specifier); - import_specifiers.push_back(*specifier_utf8); - } - - 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; -} - -void Shared(v8::Local property, - const v8::PropertyCallbackInfo& info) { - v8::Isolate* isolate = info.GetIsolate(); - DenoIsolate* d = DenoIsolate::FromIsolate(isolate); - DCHECK_EQ(d->isolate_, isolate); - v8::Locker locker(d->isolate_); - v8::EscapableHandleScope handle_scope(isolate); - if (d->shared_.data_ptr == nullptr) { - return; - } - v8::Local ab; - if (d->shared_ab_.IsEmpty()) { - // Lazily initialize the persistent external ArrayBuffer. - ab = v8::SharedArrayBuffer::New(isolate, d->shared_.data_ptr, - d->shared_.data_len, - v8::ArrayBufferCreationMode::kExternalized); - d->shared_ab_.Reset(isolate, ab); - } - info.GetReturnValue().Set(d->shared_ab_); -} - -void DenoIsolate::ClearModules() { - for (auto it = mods_.begin(); it != mods_.end(); it++) { - it->second.handle.Reset(); - } - mods_.clear(); - mods_by_name_.clear(); -} - -bool Execute(v8::Local context, const char* js_filename, - const char* js_source) { - auto* isolate = context->GetIsolate(); - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope handle_scope(isolate); - v8::Context::Scope context_scope(context); - - auto source = v8_str(js_source); - auto name = v8_str(js_filename); - - v8::TryCatch try_catch(isolate); - - v8::ScriptOrigin origin(name); - - auto script = v8::Script::Compile(context, source, &origin); - - if (script.IsEmpty()) { - DCHECK(try_catch.HasCaught()); - HandleException(context, try_catch.Exception()); - return false; - } - - auto result = script.ToLocalChecked()->Run(context); - - if (result.IsEmpty()) { - DCHECK(try_catch.HasCaught()); - HandleException(context, try_catch.Exception()); - return false; - } - - return true; -} - -static inline v8::Local v8_bool(bool v) { - return v8::Boolean::New(v8::Isolate::GetCurrent(), v); -} - -void EvalContext(const v8::FunctionCallbackInfo& args) { - v8::Isolate* isolate = args.GetIsolate(); - DenoIsolate* d = DenoIsolate::FromIsolate(isolate); - v8::EscapableHandleScope handleScope(isolate); - auto context = d->context_.Get(isolate); - v8::Context::Scope context_scope(context); - - CHECK(args[0]->IsString()); - auto source = args[0].As(); - - auto output = v8::Array::New(isolate, 2); - /** - * output[0] = result - * output[1] = ErrorInfo | null - * ErrorInfo = { - * thrown: Error | any, - * isNativeError: boolean, - * isCompileError: boolean, - * } - */ - - v8::TryCatch try_catch(isolate); - - auto name = v8_str(""); - v8::ScriptOrigin origin(name); - auto script = v8::Script::Compile(context, source, &origin); - - if (script.IsEmpty()) { - DCHECK(try_catch.HasCaught()); - auto exception = try_catch.Exception(); - - output->Set(0, v8::Null(isolate)); - - auto errinfo_obj = v8::Object::New(isolate); - errinfo_obj->Set(v8_str("isCompileError"), v8_bool(true)); - errinfo_obj->Set(v8_str("isNativeError"), - v8_bool(exception->IsNativeError())); - errinfo_obj->Set(v8_str("thrown"), exception); - - output->Set(1, errinfo_obj); - - args.GetReturnValue().Set(output); - return; - } - - auto result = script.ToLocalChecked()->Run(context); - - if (result.IsEmpty()) { - DCHECK(try_catch.HasCaught()); - auto exception = try_catch.Exception(); - - output->Set(0, v8::Null(isolate)); - - auto errinfo_obj = v8::Object::New(isolate); - errinfo_obj->Set(v8_str("isCompileError"), v8_bool(false)); - errinfo_obj->Set(v8_str("isNativeError"), - v8_bool(exception->IsNativeError())); - errinfo_obj->Set(v8_str("thrown"), exception); - - output->Set(1, errinfo_obj); - - args.GetReturnValue().Set(output); - return; - } - - output->Set(0, result.ToLocalChecked()); - output->Set(1, v8::Null(isolate)); - args.GetReturnValue().Set(output); -} - -void InitializeContext(v8::Isolate* isolate, v8::Local context) { - v8::HandleScope handle_scope(isolate); - v8::Context::Scope context_scope(context); - - auto global = context->Global(); - - auto deno_val = v8::Object::New(isolate); - CHECK(global->Set(context, deno::v8_str("Deno"), deno_val).FromJust()); - - auto core_val = v8::Object::New(isolate); - CHECK(deno_val->Set(context, deno::v8_str("core"), core_val).FromJust()); - - auto print_tmpl = v8::FunctionTemplate::New(isolate, Print); - auto print_val = print_tmpl->GetFunction(context).ToLocalChecked(); - CHECK(core_val->Set(context, deno::v8_str("print"), print_val).FromJust()); - - auto recv_tmpl = v8::FunctionTemplate::New(isolate, Recv); - auto recv_val = recv_tmpl->GetFunction(context).ToLocalChecked(); - CHECK(core_val->Set(context, deno::v8_str("recv"), recv_val).FromJust()); - - auto send_tmpl = v8::FunctionTemplate::New(isolate, Send); - auto send_val = send_tmpl->GetFunction(context).ToLocalChecked(); - CHECK(core_val->Set(context, deno::v8_str("send"), send_val).FromJust()); - - auto eval_context_tmpl = v8::FunctionTemplate::New(isolate, EvalContext); - auto eval_context_val = - eval_context_tmpl->GetFunction(context).ToLocalChecked(); - CHECK(core_val->Set(context, deno::v8_str("evalContext"), eval_context_val) - .FromJust()); - - auto error_to_json_tmpl = v8::FunctionTemplate::New(isolate, ErrorToJSON); - auto error_to_json_val = - error_to_json_tmpl->GetFunction(context).ToLocalChecked(); - CHECK(core_val->Set(context, deno::v8_str("errorToJSON"), error_to_json_val) - .FromJust()); - - CHECK(core_val->SetAccessor(context, deno::v8_str("shared"), Shared) - .FromJust()); -} - -void MessageCallback(v8::Local message, - v8::Local data) { - auto* isolate = message->GetIsolate(); - DenoIsolate* d = static_cast(isolate->GetData(0)); - - v8::HandleScope handle_scope(isolate); - auto context = d->context_.Get(isolate); - HandleExceptionMessage(context, message); -} - -void HostInitializeImportMetaObjectCallback(v8::Local context, - v8::Local module, - v8::Local meta) { - auto* isolate = context->GetIsolate(); - DenoIsolate* d = DenoIsolate::FromIsolate(isolate); - v8::Isolate::Scope isolate_scope(isolate); - - CHECK(!module.IsEmpty()); - - deno_mod id = module->GetIdentityHash(); - CHECK_NE(id, 0); - - 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) { - isolate_ = isolate; - isolate_->SetCaptureStackTraceForUncaughtExceptions( - true, 10, v8::StackTrace::kDetailed); - isolate_->SetPromiseRejectCallback(deno::PromiseRejectCallback); - isolate_->SetData(0, this); - isolate_->AddMessageListener(MessageCallback); - isolate->SetHostInitializeImportMetaObjectCallback( - HostInitializeImportMetaObjectCallback); -} - -} // namespace deno diff --git a/libdeno/deno.gni b/libdeno/deno.gni deleted file mode 100644 index 90d61240b..000000000 --- a/libdeno/deno.gni +++ /dev/null @@ -1,64 +0,0 @@ -# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import("//build/compiled_action.gni") - -# Tempalte to generate a Rollup bundle of code. -template("bundle") { - action(target_name) { - forward_variables_from(invoker, "*") - script = "//tools/run_node.py" - outputs = [ - out_dir + out_name + ".js", - out_dir + out_name + ".js.map", - ] - inputs = [ - "js/" + out_name + ".ts", - "rollup.config.js", - ] - depfile = out_dir + out_name + ".d" - args = [ - rebase_path("third_party/node_modules/rollup/bin/rollup", root_build_dir), - "-c", - rebase_path("rollup.config.js", root_build_dir), - "-i", - rebase_path(inputs[0], root_build_dir), - "-o", - rebase_path(outputs[0], root_build_dir), - "--sourcemapFile", - rebase_path("."), - "--silent", - ] - } -} - -template("run_node") { - action(target_name) { - forward_variables_from(invoker, "*") - script = "//tools/run_node.py" - } -} - -# Template to generate different V8 snapshots based on different runtime flags. -template("snapshot") { - compiled_action(target_name) { - forward_variables_from(invoker, - [ - "testonly", - "deps", - ]) - tool = "//libdeno:snapshot_creator" - visibility = [ ":*" ] # Only targets in this file can depend on this. - snapshot_out_bin = "$target_gen_dir/$target_name.bin" - inputs = [ - invoker.source_root, - ] - - outputs = [ - snapshot_out_bin, - ] - args = rebase_path(outputs, root_build_dir) + - rebase_path(inputs, root_build_dir) - - # To debug snapshotting problems: - # args += ["--trace-serializer"] - } -} diff --git a/libdeno/deno.h b/libdeno/deno.h deleted file mode 100644 index 37a302cad..000000000 --- a/libdeno/deno.h +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -#ifndef DENO_H_ -#define DENO_H_ -#include -#include -// Neither Rust nor Go support calling directly into C++ functions, therefore -// the public interface to libdeno is done in C. -#ifdef __cplusplus -extern "C" { -#endif - -// Data that gets transmitted. -typedef struct { - uint8_t* alloc_ptr; // Start of memory allocation (returned from `malloc()`). - size_t alloc_len; // Length of the memory allocation. - uint8_t* data_ptr; // Start of logical contents (within the allocation). - size_t data_len; // Length of logical contents. - size_t zero_copy_id; // 0 = normal, 1 = must call deno_zero_copy_release. -} deno_buf; - -typedef struct deno_s Deno; - -// A callback to receive a message from a libdeno.send() javascript call. -// control_buf is valid for only for the lifetime of this callback. -// data_buf is valid until deno_respond() is called. -typedef void (*denorecv_cb)(void* user_data, deno_buf control_buf, - deno_buf zerop_copy_buf); - -void deno_init(); -const char* deno_v8_version(); -void deno_set_v8_flags(int* argc, char** argv); - -typedef struct { - int will_snapshot; // Default 0. If calling deno_get_snapshot 1. - deno_buf load_snapshot; // Optionally: A deno_buf from deno_get_snapshot. - deno_buf shared; // Shared buffer to be mapped to libdeno.shared - denorecv_cb recv_cb; // Maps to libdeno.send() calls. -} deno_config; - -// Create a new deno isolate. -// Warning: If config.will_snapshot is set, deno_get_snapshot() must be called -// or an error will result. -Deno* deno_new(deno_config config); - -// Generate a snapshot. The resulting buf can be used with deno_new. -// The caller must free the returned data by calling delete[] buf.data_ptr. -deno_buf deno_get_snapshot(Deno* d); - -void deno_delete(Deno* d); - -void deno_lock(Deno* d); -void deno_unlock(Deno* d); - -// Compile and execute a traditional JavaScript script that does not use -// module import statements. -// If it succeeded deno_last_exception() will return NULL. -void deno_execute(Deno* d, void* user_data, const char* js_filename, - const char* js_source); - -// deno_respond sends up to one message back for every denorecv_cb made. -// -// If this is called during denorecv_cb, the issuing libdeno.send() in -// javascript will synchronously return the specified buf as an ArrayBuffer (or -// null if buf is empty). -// -// If this is called after denorecv_cb has returned, the deno_respond -// will call into the JS callback specified by libdeno.recv(). -// -// (Ideally, but not currently: After calling deno_respond(), the caller no -// longer owns `buf` and must not use it; deno_respond() is responsible for -// releasing its memory.) -// -// If a JS exception was encountered, deno_last_exception() will be non-NULL. -void deno_respond(Deno* d, void* user_data, deno_buf buf); - -// consumes zero_copy -// Calling this function more than once with the same zero_copy_id will result -// in an error. -void deno_zero_copy_release(Deno* d, size_t zero_copy_id); - -void deno_check_promise_errors(Deno* d); - -const char* deno_last_exception(Deno* d); - -void deno_terminate_execution(Deno* d); - -// Module API - -typedef int deno_mod; - -// Returns zero on error - check deno_last_exception(). -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); - -// Returned pointer is valid for the lifetime of the Deno isolate "d". -const char* deno_mod_imports_get(Deno* d, deno_mod id, size_t index); - -typedef deno_mod (*deno_resolve_cb)(void* user_data, const char* specifier, - deno_mod referrer); - -// If it succeeded deno_last_exception() will return NULL. -void deno_mod_instantiate(Deno* d, void* user_data, deno_mod id, - deno_resolve_cb cb); - -// If it succeeded deno_last_exception() will return NULL. -void deno_mod_evaluate(Deno* d, void* user_data, deno_mod id); - -#ifdef __cplusplus -} // extern "C" -#endif -#endif // DENO_H_ diff --git a/libdeno/exceptions.cc b/libdeno/exceptions.cc deleted file mode 100644 index 85f0ca340..000000000 --- a/libdeno/exceptions.cc +++ /dev/null @@ -1,217 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -#include "exceptions.h" -#include - -namespace deno { - -v8::Local EncodeMessageAsObject(v8::Local context, - v8::Local message) { - auto* isolate = context->GetIsolate(); - v8::EscapableHandleScope handle_scope(isolate); - v8::Context::Scope context_scope(context); - - auto stack_trace = message->GetStackTrace(); - - // Encode the exception into a JS object, which we will then turn into JSON. - auto json_obj = v8::Object::New(isolate); - auto exception_str = message->Get(); - CHECK(json_obj->Set(context, v8_str("message"), exception_str).FromJust()); - - auto maybe_source_line = message->GetSourceLine(context); - if (!maybe_source_line.IsEmpty()) { - CHECK(json_obj - ->Set(context, v8_str("sourceLine"), - maybe_source_line.ToLocalChecked()) - .FromJust()); - } - - CHECK(json_obj - ->Set(context, v8_str("scriptResourceName"), - message->GetScriptResourceName()) - .FromJust()); - - auto maybe_line_number = message->GetLineNumber(context); - if (maybe_line_number.IsJust()) { - CHECK(json_obj - ->Set(context, v8_str("lineNumber"), - v8::Integer::New(isolate, maybe_line_number.FromJust())) - .FromJust()); - } - - CHECK(json_obj - ->Set(context, v8_str("startPosition"), - v8::Integer::New(isolate, message->GetStartPosition())) - .FromJust()); - - CHECK(json_obj - ->Set(context, v8_str("endPosition"), - v8::Integer::New(isolate, message->GetEndPosition())) - .FromJust()); - - CHECK(json_obj - ->Set(context, v8_str("errorLevel"), - v8::Integer::New(isolate, message->ErrorLevel())) - .FromJust()); - - auto maybe_start_column = message->GetStartColumn(context); - if (maybe_start_column.IsJust()) { - auto start_column = - v8::Integer::New(isolate, maybe_start_column.FromJust()); - CHECK( - json_obj->Set(context, v8_str("startColumn"), start_column).FromJust()); - } - - auto maybe_end_column = message->GetEndColumn(context); - if (maybe_end_column.IsJust()) { - auto end_column = v8::Integer::New(isolate, maybe_end_column.FromJust()); - CHECK(json_obj->Set(context, v8_str("endColumn"), end_column).FromJust()); - } - - CHECK(json_obj - ->Set(context, v8_str("isSharedCrossOrigin"), - v8::Boolean::New(isolate, message->IsSharedCrossOrigin())) - .FromJust()); - - CHECK(json_obj - ->Set(context, v8_str("isOpaque"), - v8::Boolean::New(isolate, message->IsOpaque())) - .FromJust()); - - v8::Local frames; - if (!stack_trace.IsEmpty()) { - uint32_t count = static_cast(stack_trace->GetFrameCount()); - frames = v8::Array::New(isolate, count); - - for (uint32_t i = 0; i < count; ++i) { - auto frame = stack_trace->GetFrame(isolate, i); - auto frame_obj = v8::Object::New(isolate); - CHECK(frames->Set(context, i, frame_obj).FromJust()); - auto line = v8::Integer::New(isolate, frame->GetLineNumber()); - auto column = v8::Integer::New(isolate, frame->GetColumn()); - CHECK(frame_obj->Set(context, v8_str("line"), line).FromJust()); - CHECK(frame_obj->Set(context, v8_str("column"), column).FromJust()); - CHECK(frame_obj - ->Set(context, v8_str("functionName"), frame->GetFunctionName()) - .FromJust()); - // scriptName can be empty in special conditions e.g. eval - auto scriptName = frame->GetScriptNameOrSourceURL(); - if (scriptName.IsEmpty()) { - scriptName = v8_str(""); - } - CHECK( - frame_obj->Set(context, v8_str("scriptName"), scriptName).FromJust()); - CHECK(frame_obj - ->Set(context, v8_str("isEval"), - v8::Boolean::New(isolate, frame->IsEval())) - .FromJust()); - CHECK(frame_obj - ->Set(context, v8_str("isConstructor"), - v8::Boolean::New(isolate, frame->IsConstructor())) - .FromJust()); - CHECK(frame_obj - ->Set(context, v8_str("isWasm"), - v8::Boolean::New(isolate, frame->IsWasm())) - .FromJust()); - } - } else { - // No stack trace. We only have one stack frame of info.. - frames = v8::Array::New(isolate, 1); - - auto frame_obj = v8::Object::New(isolate); - CHECK(frames->Set(context, 0, frame_obj).FromJust()); - - auto line = - v8::Integer::New(isolate, message->GetLineNumber(context).FromJust()); - auto column = - v8::Integer::New(isolate, message->GetStartColumn(context).FromJust()); - - CHECK(frame_obj->Set(context, v8_str("line"), line).FromJust()); - CHECK(frame_obj->Set(context, v8_str("column"), column).FromJust()); - CHECK(frame_obj - ->Set(context, v8_str("scriptName"), - message->GetScriptResourceName()) - .FromJust()); - } - - CHECK(json_obj->Set(context, v8_str("frames"), frames).FromJust()); - json_obj = handle_scope.Escape(json_obj); - return json_obj; -} - -std::string EncodeMessageAsJSON(v8::Local context, - v8::Local message) { - auto* isolate = context->GetIsolate(); - v8::HandleScope handle_scope(isolate); - v8::Context::Scope context_scope(context); - auto json_obj = EncodeMessageAsObject(context, message); - auto json_string = v8::JSON::Stringify(context, json_obj).ToLocalChecked(); - v8::String::Utf8Value json_string_(isolate, json_string); - return std::string(ToCString(json_string_)); -} - -v8::Local EncodeExceptionAsObject(v8::Local context, - v8::Local exception) { - auto* isolate = context->GetIsolate(); - v8::EscapableHandleScope handle_scope(isolate); - v8::Context::Scope context_scope(context); - - auto message = v8::Exception::CreateMessage(isolate, exception); - auto json_obj = EncodeMessageAsObject(context, message); - json_obj = handle_scope.Escape(json_obj); - return json_obj; -} - -std::string EncodeExceptionAsJSON(v8::Local context, - v8::Local exception) { - auto* isolate = context->GetIsolate(); - v8::HandleScope handle_scope(isolate); - v8::Context::Scope context_scope(context); - - auto message = v8::Exception::CreateMessage(isolate, exception); - return EncodeMessageAsJSON(context, message); -} - -void HandleException(v8::Local context, - v8::Local exception) { - v8::Isolate* isolate = context->GetIsolate(); - - // TerminateExecution was called - if (isolate->IsExecutionTerminating()) { - // cancel exception termination so that the exception can be created - isolate->CancelTerminateExecution(); - - // maybe make a new exception object - if (exception->IsNullOrUndefined()) { - exception = v8::Exception::Error(v8_str("execution terminated")); - } - - // handle the exception as if it is a regular exception - HandleException(context, exception); - - // re-enable exception termination - isolate->TerminateExecution(); - return; - } - - DenoIsolate* d = DenoIsolate::FromIsolate(isolate); - std::string json_str = EncodeExceptionAsJSON(context, exception); - CHECK_NOT_NULL(d); - d->last_exception_ = json_str; -} - -void HandleExceptionMessage(v8::Local context, - v8::Local message) { - v8::Isolate* isolate = context->GetIsolate(); - - // TerminateExecution was called - if (isolate->IsExecutionTerminating()) { - HandleException(context, v8::Undefined(isolate)); - return; - } - - DenoIsolate* d = DenoIsolate::FromIsolate(isolate); - std::string json_str = EncodeMessageAsJSON(context, message); - CHECK_NOT_NULL(d); - d->last_exception_ = json_str; -} -} // namespace deno diff --git a/libdeno/exceptions.h b/libdeno/exceptions.h deleted file mode 100644 index e07ff183a..000000000 --- a/libdeno/exceptions.h +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -#ifndef EXCEPTIONS_H_ -#define EXCEPTIONS_H_ - -#include -#include "third_party/v8/include/v8.h" - -namespace deno { - -v8::Local EncodeExceptionAsObject(v8::Local context, - v8::Local exception); - -std::string EncodeExceptionAsJSON(v8::Local context, - v8::Local exception); - -void HandleException(v8::Local context, - v8::Local exception); - -void HandleExceptionMessage(v8::Local context, - v8::Local message); -} // namespace deno - -#endif // EXCEPTIONS_H_ diff --git a/libdeno/file_util.cc b/libdeno/file_util.cc deleted file mode 100644 index 256f4f257..000000000 --- a/libdeno/file_util.cc +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef __APPLE__ -#include -#endif - -#ifdef _WIN32 -#include -#endif - -#include "file_util.h" - -namespace deno { - -bool ReadFileToString(const char* fn, std::string* contents) { - std::ifstream file(fn, std::ios::binary); - if (file.fail()) { - return false; - } - contents->assign(std::istreambuf_iterator{file}, {}); - return !file.fail(); -} - -std::string Basename(std::string const& filename) { - for (auto it = filename.rbegin(); it != filename.rend(); ++it) { - char ch = *it; - if (ch == '\\' || ch == '/') { - return std::string(it.base(), filename.end()); - } - } - return filename; -} - -// Returns the directory component from a filename. The returned path always -// ends with a slash. This function does not understand Windows drive letters. -std::string Dirname(std::string const& filename) { - for (auto it = filename.rbegin(); it != filename.rend(); ++it) { - char ch = *it; - if (ch == '\\' || ch == '/') { - return std::string(filename.begin(), it.base()); - } - } - return std::string("./"); -} - -// Returns the full path the currently running executable. -// This implementation is very basic. Caveats: -// * OS X: fails if buffer is too small, does not retry with a bigger buffer. -// * Windows: ANSI only, no unicode. Fails if path is longer than 260 chars. -bool ExePath(std::string* path) { -#ifdef _WIN32 - // Windows only. - char exe_buf[MAX_PATH]; - DWORD len = GetModuleFileNameA(NULL, exe_buf, sizeof exe_buf); - if (len == 0 || len == sizeof exe_buf) { - return false; - } -#else -#ifdef __APPLE__ - // OS X only. - char link_buf[PATH_MAX * 2]; // Exe may be longer than MAX_PATH, says Apple. - uint32_t len = sizeof link_buf; - if (_NSGetExecutablePath(link_buf, &len) < 0) { - return false; - } -#else - // Linux only. - static const char* link_buf = "/proc/self/exe"; -#endif - // Linux and OS X. - char exe_buf[PATH_MAX]; - char* r = realpath(link_buf, exe_buf); - if (r == NULL) { - return false; - } -#endif - // All platforms. - path->assign(exe_buf); - return true; -} - -} // namespace deno diff --git a/libdeno/file_util.h b/libdeno/file_util.h deleted file mode 100644 index 0177f2c94..000000000 --- a/libdeno/file_util.h +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -#ifndef FILE_UTIL_H_ -#define FILE_UTIL_H_ - -#include - -namespace deno { -bool ReadFileToString(const char* fn, std::string* contents); -std::string Basename(std::string const& filename); -std::string Dirname(std::string const& filename); -bool ExePath(std::string* path); -} // namespace deno - -#endif // FILE_UTIL_H_ diff --git a/libdeno/file_util_test.cc b/libdeno/file_util_test.cc deleted file mode 100644 index 80c878044..000000000 --- a/libdeno/file_util_test.cc +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -#include "testing/gtest/include/gtest/gtest.h" - -#include "file_util.h" - -TEST(FileUtilTest, ReadFileToStringFileNotExist) { - std::string output; - EXPECT_FALSE(deno::ReadFileToString("/should_error_out.txt", &output)); -} - -TEST(FileUtilTest, Basename) { - EXPECT_EQ("foo.txt", deno::Basename("foo.txt")); - EXPECT_EQ("foo.txt", deno::Basename("/foo.txt")); - EXPECT_EQ("", deno::Basename("/foo/")); - EXPECT_EQ("", deno::Basename("foo/")); - EXPECT_EQ("", deno::Basename("/")); - EXPECT_EQ("foo.txt", deno::Basename(".\\foo.txt")); - EXPECT_EQ("foo.txt", deno::Basename("/home/ryan/foo.txt")); - EXPECT_EQ("foo.txt", deno::Basename("C:\\home\\ryan\\foo.txt")); -} - -TEST(FileUtilTest, Dirname) { - EXPECT_EQ("home/dank/", deno::Dirname("home/dank/memes.gif")); - EXPECT_EQ("/home/dank/", deno::Dirname("/home/dank/memes.gif")); - EXPECT_EQ("/home/dank/", deno::Dirname("/home/dank/")); - EXPECT_EQ("home/dank/", deno::Dirname("home/dank/memes.gif")); - EXPECT_EQ("/", deno::Dirname("/")); - EXPECT_EQ(".\\", deno::Dirname(".\\memes.gif")); - EXPECT_EQ("c:\\", deno::Dirname("c:\\stuff")); - EXPECT_EQ("./", deno::Dirname("nothing")); - EXPECT_EQ("./", deno::Dirname("")); -} - -TEST(FileUtilTest, ExePath) { - std::string exe_path; - EXPECT_TRUE(deno::ExePath(&exe_path)); - // Path is absolute. - EXPECT_TRUE(exe_path.find("/") == 0 || exe_path.find(":\\") == 1); - // FIlename is the name of the test binary. - std::string exe_filename = deno::Basename(exe_path); - EXPECT_EQ(exe_filename.find("test_cc"), 0u); - // Path exists (also tests ReadFileToString). - std::string contents; - EXPECT_TRUE(deno::ReadFileToString(exe_path.c_str(), &contents)); - EXPECT_NE(contents.find("Inception :)"), std::string::npos); -} diff --git a/libdeno/internal.h b/libdeno/internal.h deleted file mode 100644 index 54e845965..000000000 --- a/libdeno/internal.h +++ /dev/null @@ -1,199 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -#ifndef INTERNAL_H_ -#define INTERNAL_H_ - -#include -#include -#include -#include -#include "deno.h" -#include "third_party/v8/include/v8.h" -#include "third_party/v8/src/base/logging.h" - -namespace deno { - -struct ModuleInfo { - bool main; - std::string name; - v8::Persistent handle; - std::vector import_specifiers; - - ModuleInfo(v8::Isolate* isolate, v8::Local module, bool main_, - const char* name_, std::vector import_specifiers_) - : main(main_), name(name_), import_specifiers(import_specifiers_) { - handle.Reset(isolate, module); - } -}; - -// deno_s = Wrapped Isolate. -class DenoIsolate { - public: - explicit DenoIsolate(deno_config config) - : isolate_(nullptr), - locker_(nullptr), - shared_(config.shared), - current_args_(nullptr), - snapshot_creator_(nullptr), - global_import_buf_ptr_(nullptr), - recv_cb_(config.recv_cb), - next_zero_copy_id_(1), // zero_copy_id must not be zero. - user_data_(nullptr), - resolve_cb_(nullptr) { - array_buffer_allocator_ = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); - if (config.load_snapshot.data_ptr) { - snapshot_.data = - reinterpret_cast(config.load_snapshot.data_ptr); - snapshot_.raw_size = static_cast(config.load_snapshot.data_len); - } - } - - ~DenoIsolate() { - shared_ab_.Reset(); - if (locker_) { - delete locker_; - } - if (snapshot_creator_) { - delete snapshot_creator_; - } else { - isolate_->Dispose(); - } - delete array_buffer_allocator_; - } - - static inline DenoIsolate* FromIsolate(v8::Isolate* isolate) { - return static_cast(isolate->GetData(0)); - } - - void AddIsolate(v8::Isolate* isolate); - - deno_mod RegisterModule(bool main, const char* name, const char* source); - void ClearModules(); - - ModuleInfo* GetModuleInfo(deno_mod id) { - if (id == 0) { - return nullptr; - } - auto it = mods_.find(id); - if (it != mods_.end()) { - return &it->second; - } else { - return nullptr; - } - } - - void DeleteZeroCopyRef(size_t zero_copy_id) { - DCHECK_NE(zero_copy_id, 0); - // Delete persistent reference to data ArrayBuffer. - auto it = zero_copy_map_.find(zero_copy_id); - if (it != zero_copy_map_.end()) { - it->second.Reset(); - zero_copy_map_.erase(it); - } - } - - void AddZeroCopyRef(size_t zero_copy_id, v8::Local zero_copy_v) { - zero_copy_map_.emplace(std::piecewise_construct, - std::make_tuple(zero_copy_id), - std::make_tuple(isolate_, zero_copy_v)); - } - - v8::Isolate* isolate_; - v8::Locker* locker_; - v8::ArrayBuffer::Allocator* array_buffer_allocator_; - deno_buf shared_; - const v8::FunctionCallbackInfo* current_args_; - v8::SnapshotCreator* snapshot_creator_; - void* global_import_buf_ptr_; - denorecv_cb recv_cb_; - size_t next_zero_copy_id_; - void* user_data_; - - std::map mods_; - std::map mods_by_name_; - deno_resolve_cb resolve_cb_; - - v8::Persistent context_; - std::map> zero_copy_map_; - std::map> pending_promise_map_; - std::string last_exception_; - v8::Persistent recv_; - v8::StartupData snapshot_; - v8::Persistent global_import_buf_; - v8::Persistent shared_ab_; -}; - -class UserDataScope { - DenoIsolate* deno_; - void* prev_data_; - void* data_; // Not necessary; only for sanity checking. - - public: - UserDataScope(DenoIsolate* deno, void* data) : deno_(deno), data_(data) { - CHECK(deno->user_data_ == nullptr || deno->user_data_ == data_); - prev_data_ = deno->user_data_; - deno->user_data_ = data; - } - - ~UserDataScope() { - CHECK(deno_->user_data_ == data_); - deno_->user_data_ = prev_data_; - } -}; - -struct InternalFieldData { - uint32_t data; -}; - -static inline v8::Local v8_str(const char* x) { - return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x, - v8::NewStringType::kNormal) - .ToLocalChecked(); -} - -void Print(const v8::FunctionCallbackInfo& args); -void Recv(const v8::FunctionCallbackInfo& args); -void Send(const v8::FunctionCallbackInfo& args); -void EvalContext(const v8::FunctionCallbackInfo& args); -void ErrorToJSON(const v8::FunctionCallbackInfo& args); -void Shared(v8::Local property, - const v8::PropertyCallbackInfo& info); -void MessageCallback(v8::Local message, v8::Local data); -static intptr_t external_references[] = { - reinterpret_cast(Print), - reinterpret_cast(Recv), - reinterpret_cast(Send), - reinterpret_cast(EvalContext), - reinterpret_cast(ErrorToJSON), - reinterpret_cast(Shared), - reinterpret_cast(MessageCallback), - 0}; - -static const deno_buf empty_buf = {nullptr, 0, nullptr, 0, 0}; - -Deno* NewFromSnapshot(void* user_data, denorecv_cb cb); - -void InitializeContext(v8::Isolate* isolate, v8::Local context); - -void DeserializeInternalFields(v8::Local holder, int index, - v8::StartupData payload, void* data); - -v8::StartupData SerializeInternalFields(v8::Local holder, int index, - void* data); - -v8::Local ImportBuf(DenoIsolate* d, deno_buf buf); - -bool Execute(v8::Local context, const char* js_filename, - const char* js_source); -bool ExecuteMod(v8::Local context, const char* js_filename, - const char* js_source, bool resolve_only); - -} // namespace deno - -extern "C" { -// This is just to workaround the linker. -struct deno_s { - deno::DenoIsolate isolate; -}; -} - -#endif // INTERNAL_H_ diff --git a/libdeno/libdeno.d.ts b/libdeno/libdeno.d.ts deleted file mode 100644 index 1bc7367d9..000000000 --- a/libdeno/libdeno.d.ts +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. - -interface EvalErrorInfo { - // Is the object thrown a native Error? - isNativeError: boolean; - // Was the error happened during compilation? - isCompileError: boolean; - // The actual thrown entity - // (might be an Error or anything else thrown by the user) - // If isNativeError is true, this is an Error - // eslint-disable-next-line @typescript-eslint/no-explicit-any - thrown: any; -} - -declare interface MessageCallback { - (msg: Uint8Array): void; -} - -declare interface DenoCore { - recv(cb: MessageCallback): void; - - send( - control: null | ArrayBufferView, - data?: ArrayBufferView - ): null | Uint8Array; - - print(x: string, isErr?: boolean): void; - - shared: SharedArrayBuffer; - - /** 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]. - * If an error occurs, `output` becomes null and `errInfo` is non-null. - */ - // eslint-disable-next-line @typescript-eslint/no-explicit-any - evalContext(code: string): [any, EvalErrorInfo | null]; - - errorToJSON: (e: Error) => string; -} diff --git a/libdeno/libdeno_test.cc b/libdeno/libdeno_test.cc deleted file mode 100644 index 8949e7f4a..000000000 --- a/libdeno/libdeno_test.cc +++ /dev/null @@ -1,329 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -#include "test.h" - -TEST(LibDenoTest, InitializesCorrectly) { - EXPECT_NE(snapshot.data_ptr, nullptr); - Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); - deno_execute(d, nullptr, "a.js", "1 + 2"); - EXPECT_EQ(nullptr, deno_last_exception(d)); - deno_delete(d); -} - -TEST(LibDenoTest, Snapshotter) { - Deno* d1 = deno_new(deno_config{1, empty, empty, nullptr}); - deno_execute(d1, nullptr, "a.js", "a = 1 + 2"); - EXPECT_EQ(nullptr, deno_last_exception(d1)); - deno_buf test_snapshot = deno_get_snapshot(d1); - deno_delete(d1); - - Deno* d2 = deno_new(deno_config{0, test_snapshot, empty, nullptr}); - deno_execute(d2, nullptr, "b.js", "if (a != 3) throw Error('x');"); - EXPECT_EQ(nullptr, deno_last_exception(d2)); - deno_delete(d2); - - delete[] test_snapshot.data_ptr; -} - -TEST(LibDenoTest, CanCallFunction) { - Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); - deno_lock(d); - deno_execute(d, nullptr, "a.js", - "if (CanCallFunction() != 'foo') throw Error();"); - EXPECT_EQ(nullptr, deno_last_exception(d)); - deno_unlock(d); - deno_delete(d); -} - -TEST(LibDenoTest, ErrorsCorrectly) { - Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); - deno_execute(d, nullptr, "a.js", "throw Error()"); - EXPECT_NE(nullptr, deno_last_exception(d)); - deno_delete(d); -} - -deno_buf strbuf(const char* str) { - auto len = strlen(str); - - deno_buf buf; - buf.alloc_ptr = reinterpret_cast(strdup(str)); - buf.alloc_len = len + 1; - buf.data_ptr = buf.alloc_ptr; - buf.data_len = len; - buf.zero_copy_id = 0; - - return buf; -} - -// Same as strbuf but with null alloc_ptr. -deno_buf StrBufNullAllocPtr(const char* str) { - auto len = strlen(str); - deno_buf buf; - buf.alloc_ptr = nullptr; - buf.alloc_len = 0; - buf.data_ptr = reinterpret_cast(strdup(str)); - buf.data_len = len; - return buf; -} - -void assert_null(deno_buf b) { - EXPECT_EQ(b.alloc_ptr, nullptr); - EXPECT_EQ(b.alloc_len, 0u); - EXPECT_EQ(b.data_ptr, nullptr); - EXPECT_EQ(b.data_len, 0u); -} - -TEST(LibDenoTest, RecvReturnEmpty) { - static int count = 0; - auto recv_cb = [](auto _, auto buf, auto zero_copy_buf) { - assert_null(zero_copy_buf); - count++; - EXPECT_EQ(static_cast(3), buf.data_len); - EXPECT_EQ(buf.data_ptr[0], 'a'); - EXPECT_EQ(buf.data_ptr[1], 'b'); - EXPECT_EQ(buf.data_ptr[2], 'c'); - }; - Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb}); - deno_execute(d, nullptr, "a.js", "RecvReturnEmpty()"); - EXPECT_EQ(nullptr, deno_last_exception(d)); - EXPECT_EQ(count, 2); - deno_delete(d); -} - -TEST(LibDenoTest, RecvReturnBar) { - static int count = 0; - auto recv_cb = [](auto user_data, auto buf, auto zero_copy_buf) { - auto d = reinterpret_cast(user_data); - assert_null(zero_copy_buf); - count++; - EXPECT_EQ(static_cast(3), buf.data_len); - EXPECT_EQ(buf.data_ptr[0], 'a'); - EXPECT_EQ(buf.data_ptr[1], 'b'); - EXPECT_EQ(buf.data_ptr[2], 'c'); - EXPECT_EQ(zero_copy_buf.zero_copy_id, 0u); - EXPECT_EQ(zero_copy_buf.data_ptr, nullptr); - deno_respond(d, user_data, strbuf("bar")); - }; - Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb}); - deno_execute(d, d, "a.js", "RecvReturnBar()"); - EXPECT_EQ(nullptr, deno_last_exception(d)); - EXPECT_EQ(count, 1); - deno_delete(d); -} - -TEST(LibDenoTest, DoubleRecvFails) { - Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); - deno_execute(d, nullptr, "a.js", "DoubleRecvFails()"); - EXPECT_NE(nullptr, deno_last_exception(d)); - deno_delete(d); -} - -TEST(LibDenoTest, SendRecvSlice) { - static int count = 0; - auto recv_cb = [](auto user_data, auto buf, auto zero_copy_buf) { - auto d = reinterpret_cast(user_data); - assert_null(zero_copy_buf); - static const size_t alloc_len = 1024; - size_t i = count++; - // Check the size and offset of the slice. - size_t data_offset = buf.data_ptr - buf.alloc_ptr; - EXPECT_EQ(data_offset, i * 11); - EXPECT_EQ(buf.data_len, alloc_len - i * 30); - EXPECT_EQ(buf.alloc_len, alloc_len); - // Check values written by the JS side. - EXPECT_EQ(buf.data_ptr[0], 100 + i); - EXPECT_EQ(buf.data_ptr[buf.data_len - 1], 100 - i); - // Make copy of the backing buffer -- this is currently necessary - // because deno_respond() takes ownership over the buffer, but we are - // not given ownership of `buf` by our caller. - uint8_t* alloc_ptr = reinterpret_cast(malloc(alloc_len)); - memcpy(alloc_ptr, buf.alloc_ptr, alloc_len); - // Make a slice that is a bit shorter than the original. - deno_buf buf2{alloc_ptr, alloc_len, alloc_ptr + data_offset, - buf.data_len - 19, 0}; - // Place some values into the buffer for the JS side to verify. - buf2.data_ptr[0] = 200 + i; - buf2.data_ptr[buf2.data_len - 1] = 200 - i; - // Send back. - deno_respond(d, user_data, buf2); - }; - Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb}); - deno_execute(d, d, "a.js", "SendRecvSlice()"); - EXPECT_EQ(nullptr, deno_last_exception(d)); - EXPECT_EQ(count, 5); - deno_delete(d); -} - -TEST(LibDenoTest, JSSendArrayBufferViewTypes) { - static int count = 0; - auto recv_cb = [](auto _, auto buf, auto zero_copy_buf) { - assert_null(zero_copy_buf); - count++; - size_t data_offset = buf.data_ptr - buf.alloc_ptr; - EXPECT_EQ(data_offset, 2468u); - EXPECT_EQ(buf.data_len, 1000u); - EXPECT_EQ(buf.alloc_len, 4321u); - EXPECT_EQ(buf.data_ptr[0], count); - }; - Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb}); - deno_execute(d, nullptr, "a.js", "JSSendArrayBufferViewTypes()"); - EXPECT_EQ(nullptr, deno_last_exception(d)); - EXPECT_EQ(count, 3); - deno_delete(d); -} - -TEST(LibDenoTest, TypedArraySnapshots) { - Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); - deno_execute(d, nullptr, "a.js", "TypedArraySnapshots()"); - EXPECT_EQ(nullptr, deno_last_exception(d)); - deno_delete(d); -} - -TEST(LibDenoTest, SnapshotBug) { - Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); - deno_execute(d, nullptr, "a.js", "SnapshotBug()"); - EXPECT_EQ(nullptr, deno_last_exception(d)); - deno_delete(d); -} - -TEST(LibDenoTest, GlobalErrorHandling) { - Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); - deno_execute(d, nullptr, "a.js", "GlobalErrorHandling()"); - std::string expected = - "{\"message\":\"Uncaught ReferenceError: notdefined is not defined\"," - "\"sourceLine\":\" " - "notdefined()\",\"scriptResourceName\":\"helloworld.js\"," - "\"lineNumber\":3,\"startPosition\":3,\"endPosition\":4,\"errorLevel\":8," - "\"startColumn\":1,\"endColumn\":2,\"isSharedCrossOrigin\":false," - "\"isOpaque\":false,\"frames\":[{\"line\":3,\"column\":2," - "\"functionName\":\"\",\"scriptName\":\"helloworld.js\",\"isEval\":true," - "\"isConstructor\":false,\"isWasm\":false},"; - std::string actual(deno_last_exception(d), 0, expected.length()); - EXPECT_STREQ(expected.c_str(), actual.c_str()); - deno_delete(d); -} - -TEST(LibDenoTest, ZeroCopyBuf) { - static int count = 0; - static deno_buf zero_copy_buf2; - auto recv_cb = [](auto user_data, deno_buf buf, deno_buf zero_copy_buf) { - count++; - EXPECT_GT(zero_copy_buf.zero_copy_id, 0u); - zero_copy_buf.data_ptr[0] = 4; - zero_copy_buf.data_ptr[1] = 2; - zero_copy_buf2 = zero_copy_buf; - EXPECT_EQ(2u, buf.data_len); - EXPECT_EQ(2u, zero_copy_buf.data_len); - EXPECT_EQ(buf.data_ptr[0], 1); - EXPECT_EQ(buf.data_ptr[1], 2); - // Note zero_copy_buf won't actually be freed here because in - // libdeno_test.js zeroCopyBuf is a rooted global. We just want to exercise - // the API here. - auto d = reinterpret_cast(user_data); - deno_zero_copy_release(d, zero_copy_buf.zero_copy_id); - }; - Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb}); - deno_execute(d, d, "a.js", "ZeroCopyBuf()"); - EXPECT_EQ(nullptr, deno_last_exception(d)); - EXPECT_EQ(count, 1); - // zero_copy_buf was subsequently changed in JS, let's check that our copy - // reflects that. - EXPECT_EQ(zero_copy_buf2.data_ptr[0], 9); - EXPECT_EQ(zero_copy_buf2.data_ptr[1], 8); - deno_delete(d); -} - -TEST(LibDenoTest, CheckPromiseErrors) { - static int count = 0; - auto recv_cb = [](auto _, auto buf, auto zero_copy_buf) { count++; }; - Deno* d = deno_new(deno_config{0, snapshot, empty, recv_cb}); - EXPECT_EQ(deno_last_exception(d), nullptr); - deno_execute(d, nullptr, "a.js", "CheckPromiseErrors()"); - EXPECT_EQ(nullptr, deno_last_exception(d)); - EXPECT_EQ(deno_last_exception(d), nullptr); - EXPECT_EQ(count, 1); - // We caught the exception. So still no errors after calling - // deno_check_promise_errors(). - deno_check_promise_errors(d); - EXPECT_EQ(deno_last_exception(d), nullptr); - deno_delete(d); -} - -TEST(LibDenoTest, LastException) { - Deno* d = deno_new(deno_config{0, empty, empty, nullptr}); - EXPECT_EQ(deno_last_exception(d), nullptr); - deno_execute(d, nullptr, "a.js", "\n\nthrow Error('boo');\n\n"); - EXPECT_STREQ(deno_last_exception(d), - "{\"message\":\"Uncaught Error: boo\",\"sourceLine\":\"throw " - "Error('boo');\",\"scriptResourceName\":\"a.js\",\"lineNumber\":" - "3,\"startPosition\":8,\"endPosition\":9,\"errorLevel\":8," - "\"startColumn\":6,\"endColumn\":7,\"isSharedCrossOrigin\":" - "false,\"isOpaque\":false,\"frames\":[{\"line\":3,\"column\":7," - "\"functionName\":\"\",\"scriptName\":\"a.js\",\"isEval\":false," - "\"isConstructor\":false,\"isWasm\":false}]}"); - deno_delete(d); -} - -TEST(LibDenoTest, EncodeErrorBug) { - Deno* d = deno_new(deno_config{0, empty, empty, nullptr}); - EXPECT_EQ(deno_last_exception(d), nullptr); - deno_execute(d, nullptr, "a.js", "eval('a')"); - EXPECT_STREQ( - deno_last_exception(d), - "{\"message\":\"Uncaught ReferenceError: a is not " - "defined\",\"sourceLine\":\"a\",\"lineNumber\":1,\"startPosition\":0," - "\"endPosition\":1,\"errorLevel\":8,\"startColumn\":0,\"endColumn\":1," - "\"isSharedCrossOrigin\":false,\"isOpaque\":false,\"frames\":[{\"line\":" - "1,\"column\":1,\"functionName\":\"\",\"scriptName\":\"\"," - "\"isEval\":true,\"isConstructor\":false,\"isWasm\":false},{\"line\":1," - "\"column\":1,\"functionName\":\"\",\"scriptName\":\"a.js\",\"isEval\":" - "false,\"isConstructor\":false,\"isWasm\":false}]}"); - deno_delete(d); -} - -TEST(LibDenoTest, Shared) { - uint8_t s[] = {0, 1, 2}; - deno_buf shared = {nullptr, 0, s, 3, 0}; - Deno* d = deno_new(deno_config{0, snapshot, shared, nullptr}); - deno_execute(d, nullptr, "a.js", "Shared()"); - EXPECT_EQ(nullptr, deno_last_exception(d)); - EXPECT_EQ(s[0], 42); - EXPECT_EQ(s[1], 43); - EXPECT_EQ(s[2], 44); - deno_delete(d); -} - -TEST(LibDenoTest, Utf8Bug) { - Deno* d = deno_new(deno_config{0, empty, empty, nullptr}); - // The following is a valid UTF-8 javascript which just defines a string - // literal. We had a bug where libdeno would choke on this. - deno_execute(d, nullptr, "a.js", "x = \"\xEF\xBF\xBD\""); - EXPECT_EQ(nullptr, deno_last_exception(d)); - deno_delete(d); -} - -TEST(LibDenoTest, LibDenoEvalContext) { - Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); - deno_execute(d, nullptr, "a.js", "LibDenoEvalContext();"); - EXPECT_EQ(nullptr, deno_last_exception(d)); - deno_delete(d); -} - -TEST(LibDenoTest, LibDenoEvalContextError) { - Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr}); - deno_execute(d, nullptr, "a.js", "LibDenoEvalContextError();"); - EXPECT_EQ(nullptr, deno_last_exception(d)); - deno_delete(d); -} - -TEST(LibDenoTest, SharedAtomics) { - int32_t s[] = {0, 1, 2}; - deno_buf shared = {nullptr, 0, reinterpret_cast(s), sizeof s, 0}; - Deno* d = deno_new(deno_config{0, empty, shared, nullptr}); - deno_execute(d, nullptr, "a.js", - "Atomics.add(new Int32Array(Deno.core.shared), 0, 1)"); - EXPECT_EQ(nullptr, deno_last_exception(d)); - EXPECT_EQ(s[0], 1); - EXPECT_EQ(s[1], 1); - EXPECT_EQ(s[2], 2); - deno_delete(d); -} diff --git a/libdeno/libdeno_test.js b/libdeno/libdeno_test.js deleted file mode 100644 index b0040025d..000000000 --- a/libdeno/libdeno_test.js +++ /dev/null @@ -1,197 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. - -// A simple runtime that doesn't involve typescript or protobufs to test -// libdeno. Invoked by libdeno_test.cc - -const global = this; - -function assert(cond) { - if (!cond) throw Error("libdeno_test.js assert failed"); -} - -global.CanCallFunction = () => { - Deno.core.print("Hello world from foo"); - return "foo"; -}; - -// This object is created to test snapshotting. -// See DeserializeInternalFieldsCallback and SerializeInternalFieldsCallback. -const snapshotted = new Uint8Array([1, 3, 3, 7]); - -global.TypedArraySnapshots = () => { - assert(snapshotted[0] === 1); - assert(snapshotted[1] === 3); - assert(snapshotted[2] === 3); - assert(snapshotted[3] === 7); -}; - -global.RecvReturnEmpty = () => { - const m1 = new Uint8Array("abc".split("").map(c => c.charCodeAt(0))); - const m2 = m1.slice(); - const r1 = Deno.core.send(m1); - assert(r1 == null); - const r2 = Deno.core.send(m2); - assert(r2 == null); -}; - -global.RecvReturnBar = () => { - const m = new Uint8Array("abc".split("").map(c => c.charCodeAt(0))); - const r = Deno.core.send(m); - assert(r instanceof Uint8Array); - assert(r.byteLength === 3); - const rstr = String.fromCharCode(...r); - assert(rstr === "bar"); -}; - -global.DoubleRecvFails = () => { - // Deno.core.recv is an internal function and should only be called once from the - // runtime. - Deno.core.recv((channel, msg) => assert(false)); - Deno.core.recv((channel, msg) => assert(false)); -}; - -global.SendRecvSlice = () => { - const abLen = 1024; - let buf = new Uint8Array(abLen); - for (let i = 0; i < 5; i++) { - // Set first and last byte, for verification by the native side. - buf[0] = 100 + i; - buf[buf.length - 1] = 100 - i; - // On the native side, the slice is shortened by 19 bytes. - buf = Deno.core.send(buf); - assert(buf.byteOffset === i * 11); - assert(buf.byteLength === abLen - i * 30 - 19); - assert(buf.buffer.byteLength == abLen); - // Look for values written by the backend. - assert(buf[0] === 200 + i); - assert(buf[buf.length - 1] === 200 - i); - // On the JS side, the start of the slice is moved up by 11 bytes. - buf = buf.subarray(11); - assert(buf.byteOffset === (i + 1) * 11); - assert(buf.byteLength === abLen - (i + 1) * 30); - } -}; - -global.JSSendArrayBufferViewTypes = () => { - // Test that ArrayBufferView slices are transferred correctly. - // Send Uint8Array. - const ab1 = new ArrayBuffer(4321); - const u8 = new Uint8Array(ab1, 2468, 1000); - u8[0] = 1; - Deno.core.send(u8); - // Send Uint32Array. - const ab2 = new ArrayBuffer(4321); - const u32 = new Uint32Array(ab2, 2468, 1000 / Uint32Array.BYTES_PER_ELEMENT); - u32[0] = 0x02020202; - Deno.core.send(u32); - // Send DataView. - const ab3 = new ArrayBuffer(4321); - const dv = new DataView(ab3, 2468, 1000); - dv.setUint8(0, 3); - Deno.core.send(dv); -}; - -// The following join has caused SnapshotBug to segfault when using kKeep. -[].join(""); - -global.SnapshotBug = () => { - assert("1,2,3" === String([1, 2, 3])); -}; - -global.GlobalErrorHandling = () => { - eval("\n\n notdefined()\n//# sourceURL=helloworld.js"); -}; - -// Allocate this buf at the top level to avoid GC. -const zeroCopyBuf = new Uint8Array([3, 4]); - -global.ZeroCopyBuf = () => { - const a = new Uint8Array([1, 2]); - const b = zeroCopyBuf; - // The second parameter of send should modified by the - // privileged side. - const r = Deno.core.send(a, b); - assert(r == null); - // b is different. - assert(b[0] === 4); - assert(b[1] === 2); - // Now we modify it again. - b[0] = 9; - b[1] = 8; -}; - -global.CheckPromiseErrors = () => { - async function fn() { - throw new Error("message"); - } - - (async () => { - try { - await fn(); - } catch (e) { - Deno.core.send(new Uint8Array([42])); - } - })(); -}; - -global.Shared = () => { - const ab = Deno.core.shared; - assert(ab instanceof SharedArrayBuffer); - assert(Deno.core.shared != undefined); - assert(ab.byteLength === 3); - const ui8 = new Uint8Array(ab); - assert(ui8[0] === 0); - assert(ui8[1] === 1); - assert(ui8[2] === 2); - ui8[0] = 42; - ui8[1] = 43; - ui8[2] = 44; -}; - -global.LibDenoEvalContext = () => { - const [result, errInfo] = Deno.core.evalContext("let a = 1; a"); - assert(result === 1); - assert(!errInfo); - const [result2, errInfo2] = Deno.core.evalContext("a = a + 1; a"); - assert(result2 === 2); - assert(!errInfo2); -}; - -global.LibDenoEvalContextError = () => { - const [result, errInfo] = Deno.core.evalContext("not_a_variable"); - assert(!result); - assert(!!errInfo); - assert(errInfo.isNativeError); // is a native error (ReferenceError) - assert(!errInfo.isCompileError); // is NOT a compilation error - assert(errInfo.thrown.message === "not_a_variable is not defined"); - - const [result2, errInfo2] = Deno.core.evalContext("throw 1"); - assert(!result2); - assert(!!errInfo2); - assert(!errInfo2.isNativeError); // is NOT a native error - assert(!errInfo2.isCompileError); // is NOT a compilation error - assert(errInfo2.thrown === 1); - - const [result3, errInfo3] = Deno.core.evalContext( - "class AError extends Error {}; throw new AError('e')" - ); - assert(!result3); - assert(!!errInfo3); - assert(errInfo3.isNativeError); // extend from native error, still native error - assert(!errInfo3.isCompileError); // is NOT a compilation error - assert(errInfo3.thrown.message === "e"); - - const [result4, errInfo4] = Deno.core.evalContext("{"); - assert(!result4); - assert(!!errInfo4); - assert(errInfo4.isNativeError); // is a native error (SyntaxError) - assert(errInfo4.isCompileError); // is a compilation error! (braces not closed) - assert(errInfo4.thrown.message === "Unexpected end of input"); - - const [result5, errInfo5] = Deno.core.evalContext("eval('{')"); - assert(!result5); - assert(!!errInfo5); - assert(errInfo5.isNativeError); // is a native error (SyntaxError) - assert(!errInfo5.isCompileError); // is NOT a compilation error! (just eval) - assert(errInfo5.thrown.message === "Unexpected end of input"); -}; diff --git a/libdeno/modules.cc b/libdeno/modules.cc deleted file mode 100644 index 0b408aec8..000000000 --- a/libdeno/modules.cc +++ /dev/null @@ -1,154 +0,0 @@ -// Copyright 2018 the Deno authors. All rights reserved. MIT license. -#include "exceptions.h" -#include "internal.h" - -using deno::DenoIsolate; -using deno::HandleException; -using v8::Boolean; -using v8::Context; -using v8::EscapableHandleScope; -using v8::HandleScope; -using v8::Integer; -using v8::Isolate; -using v8::Local; -using v8::Locker; -using v8::Module; -using v8::Object; -using v8::ScriptCompiler; -using v8::ScriptOrigin; -using v8::String; -using v8::Value; - -v8::MaybeLocal ResolveCallback(Local context, - Local specifier, - Local referrer) { - auto* isolate = context->GetIsolate(); - v8::Isolate::Scope isolate_scope(isolate); - v8::Locker locker(isolate); - - DenoIsolate* d = DenoIsolate::FromIsolate(isolate); - - v8::EscapableHandleScope handle_scope(isolate); - - deno_mod referrer_id = referrer->GetIdentityHash(); - auto* referrer_info = d->GetModuleInfo(referrer_id); - CHECK_NOT_NULL(referrer_info); - - for (int i = 0; i < referrer->GetModuleRequestsLength(); i++) { - Local req = referrer->GetModuleRequest(i); - - if (req->Equals(context, specifier).ToChecked()) { - v8::String::Utf8Value req_utf8(isolate, req); - std::string req_str(*req_utf8); - - 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. - auto* info = d->GetModuleInfo(id); - if (info == nullptr) { - char buf[64 * 1024]; - snprintf(buf, sizeof(buf), "Cannot resolve module \"%s\" from \"%s\"", - req_str.c_str(), referrer_info->name.c_str()); - isolate->ThrowException(deno::v8_str(buf)); - break; - } else { - Local child_mod = info->handle.Get(isolate); - return handle_scope.Escape(child_mod); - } - } - } - - return v8::MaybeLocal(); // Error -} - -extern "C" { - -deno_mod deno_mod_new(Deno* d_, bool main, const char* name_cstr, - const char* source_cstr) { - auto* d = unwrap(d_); - return d->RegisterModule(main, name_cstr, source_cstr); -} - -const char* deno_mod_name(Deno* d_, deno_mod id) { - auto* d = unwrap(d_); - auto* info = d->GetModuleInfo(id); - return info->name.c_str(); -} - -size_t deno_mod_imports_len(Deno* d_, deno_mod id) { - auto* d = unwrap(d_); - auto* info = d->GetModuleInfo(id); - return info->import_specifiers.size(); -} - -const char* deno_mod_imports_get(Deno* d_, deno_mod id, size_t index) { - auto* d = unwrap(d_); - auto* info = d->GetModuleInfo(id); - if (info == nullptr || index >= info->import_specifiers.size()) { - return nullptr; - } else { - return info->import_specifiers[index].c_str(); - } -} - -void deno_mod_instantiate(Deno* d_, void* user_data, deno_mod id, - deno_resolve_cb cb) { - auto* d = unwrap(d_); - deno::UserDataScope user_data_scope(d, user_data); - - auto* isolate = d->isolate_; - v8::Isolate::Scope isolate_scope(isolate); - v8::Locker locker(isolate); - v8::HandleScope handle_scope(isolate); - auto context = d->context_.Get(d->isolate_); - v8::Context::Scope context_scope(context); - - v8::TryCatch try_catch(isolate); - { - CHECK_NULL(d->resolve_cb_); - d->resolve_cb_ = cb; - { - auto* info = d->GetModuleInfo(id); - if (info == nullptr) { - return; - } - Local module = info->handle.Get(isolate); - if (module->GetStatus() == Module::kErrored) { - return; - } - auto maybe_ok = module->InstantiateModule(context, ResolveCallback); - CHECK(maybe_ok.IsJust() || try_catch.HasCaught()); - } - d->resolve_cb_ = nullptr; - } - - if (try_catch.HasCaught()) { - HandleException(context, try_catch.Exception()); - } -} - -void deno_mod_evaluate(Deno* d_, void* user_data, deno_mod id) { - auto* d = unwrap(d_); - deno::UserDataScope user_data_scope(d, user_data); - - auto* isolate = d->isolate_; - v8::Isolate::Scope isolate_scope(isolate); - v8::Locker locker(isolate); - v8::HandleScope handle_scope(isolate); - auto context = d->context_.Get(d->isolate_); - v8::Context::Scope context_scope(context); - - auto* info = d->GetModuleInfo(id); - Local module = info->handle.Get(isolate); - - CHECK_EQ(Module::kInstantiated, module->GetStatus()); - - auto maybe_result = module->Evaluate(context); - if (maybe_result.IsEmpty()) { - CHECK_EQ(Module::kErrored, module->GetStatus()); - HandleException(context, module->GetException()); - } -} - -} // extern "C" diff --git a/libdeno/modules_test.cc b/libdeno/modules_test.cc deleted file mode 100644 index 9f9228430..000000000 --- a/libdeno/modules_test.cc +++ /dev/null @@ -1,148 +0,0 @@ -// Copyright 2018 the Deno authors. All rights reserved. MIT license. -#include "test.h" - -static int exec_count = 0; -void recv_cb(void* user_data, deno_buf buf, deno_buf zero_copy_buf) { - // We use this to check that scripts have executed. - EXPECT_EQ(1u, buf.data_len); - EXPECT_EQ(buf.data_ptr[0], 4); - EXPECT_EQ(zero_copy_buf.zero_copy_id, 0u); - EXPECT_EQ(zero_copy_buf.data_ptr, nullptr); - exec_count++; -} - -TEST(ModulesTest, Resolution) { - exec_count = 0; // Reset - Deno* d = deno_new(deno_config{0, empty, empty, recv_cb}); - EXPECT_EQ(0, exec_count); - - static deno_mod a = deno_mod_new(d, true, "a.js", - "import { b } from 'b.js'\n" - "if (b() != 'b') throw Error();\n" - "Deno.core.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(1u, 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_EQ(nullptr, deno_mod_imports_get(d, a, 1)); - 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, ResolutionError) { - exec_count = 0; // Reset - Deno* d = deno_new(deno_config{0, empty, empty, recv_cb}); - EXPECT_EQ(0, exec_count); - - static deno_mod a = deno_mod_new(d, true, "a.js", - "import 'bad'\n" - "Deno.core.send(new Uint8Array([4]));"); - EXPECT_NE(a, 0); - EXPECT_EQ(nullptr, deno_last_exception(d)); - - EXPECT_EQ(0, exec_count); - - EXPECT_EQ(1u, deno_mod_imports_len(d, a)); - EXPECT_STREQ("bad", deno_mod_imports_get(d, a, 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, "bad"); - resolve_count++; - return 0; - }; - - deno_mod_instantiate(d, d, a, resolve_cb); - EXPECT_NE(nullptr, deno_last_exception(d)); - EXPECT_EQ(1, resolve_count); - EXPECT_EQ(0, exec_count); - - deno_delete(d); -} - -TEST(ModulesTest, ImportMetaUrl) { - exec_count = 0; // Reset - Deno* d = deno_new(deno_config{0, empty, empty, recv_cb}); - EXPECT_EQ(0, exec_count); - - static deno_mod a = - deno_mod_new(d, true, "a.js", - "if ('a.js' != import.meta.url) throw 'hmm'\n" - "Deno.core.send(new Uint8Array([4]));"); - EXPECT_NE(a, 0); - EXPECT_EQ(nullptr, deno_last_exception(d)); - - 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); -} - -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); -} diff --git a/libdeno/snapshot_creator.cc b/libdeno/snapshot_creator.cc deleted file mode 100644 index 19098392d..000000000 --- a/libdeno/snapshot_creator.cc +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -// Hint: --trace_serializer is a useful debugging flag. -#include -#include -#include "deno.h" -#include "file_util.h" -#include "internal.h" -#include "third_party/v8/include/v8.h" -#include "third_party/v8/src/base/logging.h" - -namespace deno {} // namespace deno - -int main(int argc, char** argv) { - const char* snapshot_out_bin = argv[1]; - const char* js_fn = argv[2]; - - deno_set_v8_flags(&argc, argv); - - CHECK_NOT_NULL(js_fn); - CHECK_NOT_NULL(snapshot_out_bin); - - std::string js_source; - CHECK(deno::ReadFileToString(js_fn, &js_source)); - - deno_init(); - deno_config config = {1, deno::empty_buf, deno::empty_buf, nullptr}; - Deno* d = deno_new(config); - - deno_execute(d, nullptr, js_fn, js_source.c_str()); - if (deno_last_exception(d) != nullptr) { - std::cerr << "Snapshot Exception " << std::endl; - std::cerr << deno_last_exception(d) << std::endl; - deno_delete(d); - return 1; - } - - auto snapshot = deno_get_snapshot(d); - - std::ofstream file_(snapshot_out_bin, std::ios::binary); - file_.write(reinterpret_cast(snapshot.data_ptr), snapshot.data_len); - file_.close(); - - delete[] snapshot.data_ptr; - deno_delete(d); - - return file_.bad(); -} diff --git a/libdeno/test.cc b/libdeno/test.cc deleted file mode 100644 index 1340fe8c3..000000000 --- a/libdeno/test.cc +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -#include "test.h" -#include -#include "file_util.h" - -deno_buf snapshot = {nullptr, 0, nullptr, 0, 0}; - -int main(int argc, char** argv) { - // Locate the snapshot. - std::string exe_path; - if (!deno::ExePath(&exe_path)) { - std::cerr << "deno::ExePath() failed" << std::endl; - return 1; - } - std::string snapshot_path = deno::Dirname(exe_path) + SNAPSHOT_PATH; - - // Load the snapshot. - std::string contents; - if (!deno::ReadFileToString(snapshot_path.c_str(), &contents)) { - std::cerr << "Failed to read snapshot from " << snapshot_path << std::endl; - return 1; - } - snapshot.data_ptr = - reinterpret_cast(const_cast(contents.c_str())); - snapshot.data_len = contents.size(); - - testing::InitGoogleTest(&argc, argv); - deno_init(); - deno_set_v8_flags(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/libdeno/test.h b/libdeno/test.h deleted file mode 100644 index 2f7c32384..000000000 --- a/libdeno/test.h +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -#ifndef TEST_H_ -#define TEST_H_ - -#include "deno.h" -#include "testing/gtest/include/gtest/gtest.h" - -extern deno_buf snapshot; // Loaded in libdeno/test.cc -const deno_buf empty = {nullptr, 0, nullptr, 0, 0}; - -#endif // TEST_H_ diff --git a/rollup.config.js b/rollup.config.js index ad3d9059b..0907ba737 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -18,7 +18,7 @@ const typescriptPath = path.resolve( __dirname, "third_party/node_modules/typescript/lib/typescript.js" ); -const gnArgs = fs.readFileSync("gen/gn_args.txt", "utf-8").trim(); +const gnArgs = fs.readFileSync("gen/cli/gn_args.txt", "utf-8").trim(); // We will allow generated modules to be resolvable by TypeScript based on // the current build path @@ -96,7 +96,7 @@ function resolveGenerated() { return { name: "resolve-msg-generated", resolveId(importee) { - if (importee.startsWith("gen/msg_generated")) { + if (importee.startsWith("gen/cli/msg_generated")) { return path.resolve(`${importee}.ts`); } } diff --git a/tools/benchmark.py b/tools/benchmark.py index cf2bc4ff7..772e35d40 100755 --- a/tools/benchmark.py +++ b/tools/benchmark.py @@ -60,17 +60,17 @@ def get_binary_sizes(build_dir): "deno": os.path.join(build_dir, "deno" + executable_suffix), "main.js": - os.path.join(build_dir, "gen/bundle/main.js"), + os.path.join(build_dir, "gen/cli/bundle/main.js"), "main.js.map": - os.path.join(build_dir, "gen/bundle/main.js.map"), + os.path.join(build_dir, "gen/cli/bundle/main.js.map"), "compiler.js": - os.path.join(build_dir, "gen/bundle/compiler.js"), + os.path.join(build_dir, "gen/cli/bundle/compiler.js"), "compiler.js.map": - os.path.join(build_dir, "gen/bundle/compiler.js.map"), + os.path.join(build_dir, "gen/cli/bundle/compiler.js.map"), "snapshot_deno.bin": - os.path.join(build_dir, "gen/snapshot_deno.bin"), + os.path.join(build_dir, "gen/cli/snapshot_deno.bin"), "snapshot_compiler.bin": - os.path.join(build_dir, "gen/snapshot_compiler.bin") + os.path.join(build_dir, "gen/cli/snapshot_compiler.bin") } sizes = {} for name, path in path_dict.items(): diff --git a/tools/format.py b/tools/format.py index bc6a3b532..b0d3bc5e4 100755 --- a/tools/format.py +++ b/tools/format.py @@ -21,10 +21,10 @@ def qrun(cmd, env=None): print "clang_format" qrun([clang_format_path, "-i", "-style", "Google"] + - find_exts(["libdeno"], [".cc", ".h"])) + find_exts(["core"], [".cc", ".h"])) print "gn format" -for fn in ["BUILD.gn", ".gn"] + find_exts(["build_extra", "libdeno"], +for fn in ["BUILD.gn", ".gn"] + find_exts(["build_extra", "core"], [".gn", ".gni"]): qrun(["third_party/depot_tools/gn", "format", fn], env=google_env()) @@ -43,6 +43,7 @@ qrun(["node", prettier, "--write", "--loglevel=error"] + ["rollup.config.js"] + print "rustfmt" qrun([ - "third_party/rustfmt/" + platform() + - "/rustfmt", "--config-path", rustfmt_config, "build.rs" + "third_party/rustfmt/" + platform() + "/rustfmt", + "--config-path", + rustfmt_config, ] + find_exts(["cli", "core"], [".rs"])) diff --git a/tools/lint.py b/tools/lint.py index 4ce7900d1..6f6ba8dfe 100755 --- a/tools/lint.py +++ b/tools/lint.py @@ -16,7 +16,7 @@ eslint = os.path.join(third_party_path, "node_modules", "eslint", "bin", os.chdir(root_path) run([ "python", cpplint, "--filter=-build/include_subdir", - "--repository=libdeno", "--extensions=cc,h", "--recursive", "libdeno" + "--repository=core/libdeno", "--extensions=cc,h", "--recursive", "core" ]) run([ diff --git a/tools/ts_library_builder/build_library.ts b/tools/ts_library_builder/build_library.ts index c2d0ef109..b0d438531 100644 --- a/tools/ts_library_builder/build_library.ts +++ b/tools/ts_library_builder/build_library.ts @@ -80,7 +80,7 @@ const libPreamble = `// Copyright 2018-2019 the Deno authors. All rights reserve `; // The path to the msg_generated file relative to the build path -const MSG_GENERATED_PATH = "/gen/msg_generated.ts"; +const MSG_GENERATED_PATH = "/gen/cli/msg_generated.ts"; // An array of enums we want to expose pub const MSG_GENERATED_ENUMS = ["ErrorKind"]; diff --git a/tools/ts_library_builder/main.ts b/tools/ts_library_builder/main.ts index ed692d03a..54a659d01 100644 --- a/tools/ts_library_builder/main.ts +++ b/tools/ts_library_builder/main.ts @@ -5,7 +5,7 @@ import { main as buildRuntimeLib } from "./build_library"; // this is very simplistic argument parsing, just enough to integrate into // the build scripts, versus being very robust let basePath = process.cwd(); -let buildPath = path.join(basePath, "out", "debug"); +let buildPath = path.join(basePath, "target", "debug"); let outFile = path.join(buildPath, "gen", "lib", "lib.d.ts"); let inline: string[] = []; let debug = false; diff --git a/tsconfig.json b/tsconfig.json index e79023de3..500b0fcd0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,7 +25,7 @@ "files": [ "js/lib.web_assembly.d.ts", "core/core.d.ts", - "libdeno/libdeno.d.ts", + "core/libdeno/libdeno.d.ts", "js/main.ts", "js/compiler.ts" ] -- cgit v1.2.3