Giter VIP home page Giter VIP logo

rusty_v8's Introduction

Rusty V8 Binding

V8 Version: 12.4.254.12

ci crates docs

Goals

  1. Provide high quality Rust bindings to V8's C++ API. The API should match the original API as closely as possible.

  2. Do not introduce additional call overhead. (For example, previous attempts at Rust V8 bindings forced the use of Persistent handles.)

  3. Do not rely on a binary libv8.a built outside of cargo. V8 is a very large project (over 600,000 lines of C++) which often takes 30 minutes to compile. Furthermore, V8 relies on Chromium's bespoke build system (gn + ninja) which is not easy to use outside of Chromium. For this reason many attempts to bind to V8 rely on pre-built binaries that are built separately from the binding itself. While this is simple, it makes upgrading V8 difficult, it makes CI difficult, it makes producing builds with different configurations difficult, and it is a security concern since binary blobs can hide malicious code. For this reason we believe it is imperative to build V8 from source code during "cargo build".

  4. Publish the crate on crates.io and allow docs.rs to generate documentation. Due to the complexity and size of V8's build, this is nontrivial. For example the crate size must be kept under 10 MiB in order to publish.

Binary Build

V8 is very large and takes a long time to compile. Many users will prefer to use a prebuilt version of V8. We publish static libs for every version of rusty v8 on Github.

Binaries builds are turned on by default: cargo build will initiate a download from github to get the static lib. To disable this build using the V8_FROM_SOURCE environmental variable.

When making changes to rusty_v8 itself, it should be tested by build from source. The CI always builds from source.

The V8_FORCE_DEBUG environment variable

By default rusty_v8 will link against release builds of v8, if you want to use a debug build of v8 set V8_FORCE_DEBUG=true.

We default to release builds of v8 due to performance & CI reasons in deno.

The RUSTY_V8_MIRROR environment variable

Tells the build script where to get binary builds from. Understands http:// and https:// URLs, and file paths. The default is https://github.com/denoland/rusty_v8/releases.

File-based mirrors are good for using cached downloads. First, point the environment variable to a suitable location:

# you might want to add this to your .bashrc
$ export RUSTY_V8_MIRROR=$HOME/.cache/rusty_v8

Then populate the cache:

#!/bin/bash

# see https://github.com/denoland/rusty_v8/releases

for REL in v0.13.0 v0.12.0; do
  mkdir -p $RUSTY_V8_MIRROR/$REL
  for FILE in \
    librusty_v8_debug_x86_64-unknown-linux-gnu.a \
    librusty_v8_release_x86_64-unknown-linux-gnu.a \
  ; do
    if [ ! -f $RUSTY_V8_MIRROR/$REL/$FILE ]; then
      wget -O $RUSTY_V8_MIRROR/$REL/$FILE \
        https://github.com/denoland/rusty_v8/releases/download/$REL/$FILE
    fi
  done
done

The RUSTY_V8_ARCHIVE environment variable

Tell the build script to use a specific v8 library. This can be an URL or a path. This is useful when you have a prebuilt archive somewhere:

export RUSTY_V8_ARCHIVE=/path/to/custom_archive.a
cargo build

Build V8 from Source

Use V8_FROM_SOURCE=1 cargo build -vv to build the crate completely from source.

The build scripts require Python 3 to be available as python3 in your PATH. If you want to specify the exact binary of Python to use, you should use the PYTHON environment variable.

The build also requires curl to be installed on your system.

For linux builds: glib-2.0 development files need to be installed such that pkg-config can find them. On Ubuntu, run sudo apt install libglib2.0-dev to install them.

For Windows builds: the 64-bit toolchain needs to be used. 32-bit targets are not supported.

For Mac builds: You'll need Xcode and Xcode CLT installed. Recent macOS versions will also require you to pass PYTHON=python3 because macOS no longer ships with python simlinked to Python 3.

The build depends on several binary tools: gn, ninja and clang. The tools will automatically be downloaded, if they are not detected in the environment.

Specifying the $GN and $NINJA environmental variables can be used to skip the download of gn and ninja. The clang download can be skipped by setting $CLANG_BASE_PATH to the directory containing a llvm/clang installation. V8 is known to rely on bleeding edge features, so LLVM v8.0+ or Apple clang 11.0+ is recommended.

Arguments can be passed to gn by setting the $GN_ARGS environmental variable.

Env vars used in when building from source: SCCACHE, CCACHE, GN, NINJA, CLANG_BASE_PATH, GN_ARGS

FAQ

Building V8 takes over 30 minutes, this is too slow for me to use this crate. What should I do?

Install sccache or ccache. Our build scripts will detect and use them. Set the $SCCACHE or $CCACHE environmental variable if it's not in your path.

What are all these random directories for like build and buildtools are these really necessary?

In order to build V8 from source code, we must provide a certain directory structure with some git submodules from Chromium. We welcome any simplifications to the code base, but this is a structure we have found after many failed attempts that carefully balances the requirements of cargo crates and GN/Ninja.

V8 has a very large API with hundreds of methods. Why don't you automate the generation of this binding code?

We have actually started down this route several times, however due to many eccentric features of the V8 API, this has not proven successful. Therefore we are proceeding in a brute-force fashion for now, focusing on solving our stated goals first. We hope to auto-generate bindings in the future.

Why are you building this?

This is to support the Deno project. We previously have gotten away with a simpler high-level Rust binding to V8 called libdeno. But as Deno has matured we've found ourselves continually needing access to an increasing amount of V8's API in Rust.

When building I get unknown argument: '-gno-inline-line-tables'

Use export GN_ARGS="no_inline_line_tables=false" during build.

My program crashes when initializing on non-main thread

Initializing V8 on a non-main thread with the CPUs PKU feature enabled might lead to crashes. You can work around this problem by using v8::new_unprotected_default_platform.

See #1381

Download cache

The v8 archives used for linking in prebuilt mode can be cached to avoid re-downloading archives when switching between branches that otherwise change the current rusty_v8 version.

To populate the cache by hand, you'll need to place the files in the appropriate location in your .cargo folder. Running cargo build -v -v will print two lines that you can use to determine the correct file and cache location:

[v8 0.87.0] static lib URL: https://github.com/denoland/rusty_v8/releases/download/v0.87.0/librusty_v8_release_aarch64-apple-darwin.a.gz
[v8 0.87.0] Looking for download in '"/Users/<name>/.cargo/.rusty_v8/https___github_com_denoland_rusty_v8_releases_download_v0_87_0_librusty_v8_release_aarch64_apple_darwin_a_gz"'

Given the above log output, use curl to download the file like so:

curl -L https://github.com/denoland/rusty_v8/releases/download/v0.87.0/librusty_v8_release_aarch64-apple-darwin.a.gz >
  /Users/<name>/.cargo/.rusty_v8/https___github_com_denoland_rusty_v8_releases_download_v0_87_0_librusty_v8_release_aarch64_apple_darwin_a_gz

For maintainers

Cut a release

Create a PR to bump the release version (e.g. #1415).

Create a new release/tag after the bump PR is landed. CI will publish the crate and upload release binaries. You will need to manually upload binary archives for M1 build.

$ V8_FROM_SOURCE=1 cargo build
$ V8_FROM_SOURCE=1 cargo build --release

rusty_v8's People

Contributors

aapoalas avatar aarono avatar afinch7 avatar andreubotella avatar bakjos avatar bartlomieju avatar bnoordhuis avatar crowlkats avatar danbev avatar denobot avatar dependabot[bot] avatar devsnek avatar espindola avatar github-actions[bot] avatar guillaumebort avatar igorzi avatar inteon avatar legendecas avatar littledivy avatar losfair avatar lucacasonato avatar magurotuna avatar mmastrac avatar neolegends avatar perfectlaugh avatar piscisaureus avatar romainmuller avatar ry avatar skylerlipthay avatar wg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rusty_v8's Issues

Replace get_data/set_data with safe alternative

Probably something like this works:

fn get_data<T>(&self, slot: u32) -> Option<&T>;
fn get_data_mut<T>(&mut self, slot: u32) -> Option<&mut T>;
fn set_data<T>(&mut self, slot: u32, data: T);

An initial (heavy handed) attempt at this was done in #282 but it was rolled back in eba98e7.

Need test that shows slot objects get their drop handlers called when Isolate is destroyed.

Need automated conversion for Local<String> to Local<Value>

Here is an attempted solution which doesn't quite work.

/// For example, to allow automated conversion from Local<String> to
/// Local<Value>
impl<'sc, T, U> From<Local<'sc, T>> for Local<'sc, U>
where
  T: Deref<Target = U>,
{
  fn from(v: Local<'sc, T>) -> Local<'sc, U> {
    unsafe { std::mem::transmute(v) }
  }
}

In C++ this is done by this bit of code: https://github.com/v8/v8/blob/63da8397bc967232f91fea07b1861a50d20ca717/include/v8.h#L192-L201

Implicitly enter Isolate in v8::error()

Exceptions is one of few APIs that still require to enter isolate before calling constructor method (as they internally rely on Isolate::GetCurrent().

Since we already have scope argument in each of the methods used to create different errors I think we might want to implicitly enter/exit isolate when creating errors:

// now
pub fn error<'sc>(
  scope: &mut impl ToLocal<'sc>,
  mut message: Local<String>,
) -> Local<'sc, Value> {
  unsafe { scope.to_local(v8__Exception__Error(&mut *message)) }.unwrap()
}

// proposed change
pub fn error<'sc>(
  scope: &mut impl ToLocal<'sc>,
  mut message: Local<String>,
) -> Local<'sc, Value> {
  let isolate = scope.isolate();
  isolate.enter();
  let e = unsafe { scope.to_local(v8__Exception__Error(&mut *message)) }.unwrap();
  isolate.exit();
  e
}

CC @ry @piscisaureus

Project description

I think the repo description on Github should be "V8 bindings for Rust", not "V8 javascript bindings for Rust". The latter can be interpret as bindings for rust and javascript?

support arm platforms

I've done a little research on this. It looks like cross compiling to arm64 works fine, but it requires some setup and small changes:

  • Sysroot usage is required (need to remove use_sysroot = false from gn options when compiling to arm)
  • target_cpu needs to be set correctly (and maybe v8_target_cpu not sure yet)

Other than these and installing the correct toolchain it's not to hard to compile for arm64 successfully. I would like to get a build with tests running in CI for at least arm64 and maybe other arm platforms if possible.

APIs required to implement inspector

  • v8_inspector::V8Inspector
    • create
    • connect
    • StringView
  • v8_inspector::ContextInfo
    • new
  • v8_inspector::InspectorSession
    • dispatchProtocolMessage
    • schedulePauseOnNextStatement
  • API to get Rust String out of StringView if it is two-byte string (!string_view.is_8_bit())

CC @piscisaureus

Peculiar build error from func types

This code causes a strange build error:

let func: fn(&mut v8::HandleScope, v8::FunctionCallbackArguments, v8::ReturnValue) = someFunction;                                                                                                             
v8::Function::new(scope, func).unwrap();    
error[E0080]: could not evaluate constant
   --> /home/snek/.cargo/registry/src/github.com-1ecc6299db9ec823/rusty_v8-0.9.0/src/support.rs:490:5
    |
490 |     [s][size_must_be_0]
    |     ^^^^^^^^^^^^^^^^^^^ index out of bounds: the len is 1 but the index is 8

error[E0080]: could not evaluate constant
   --> /home/snek/.cargo/registry/src/github.com-1ecc6299db9ec823/rusty_v8-0.9.0/src/support.rs:484:22
    |
484 |   const SELF: Self = Self::new_checked();
    |                      ^^^^^^^^^^^^^^^^^^^ referenced constant has errors

error: aborting due to 2 previous errors; 1 warning emitted

In practice the usage looks like this:

static BINDINGS: &[
  (&str, fn(&mut v8::HandleScope, v8::FunctionCallbackArguments, v8::ReturnValue))
] = &[
  ("something", something),
  ("somethingElse", something_else),
];

for (name, f) in BINDINGS {
  let f = v8::Function::new(scope, f).unwrap();
  // ...
}

Add support for GetHeapStatistics

Can you add support for GetHeapStatistics. I need to monitor the isolate memory usage. I try to implement it my self but failed. My c++ and binding is not great.

binding.cc

void v8__Isolate__GetHeapStatistics(v8::Isolate* isolate) {
  v8::HeapStatistics stats;
  return isolate->GetHeapStatistics(&stats);
}

isolate.rs

extern "C" {
  fn v8__Isolate__GetHeapStatistics(isolate: *mut Isolate);
}
/// Get statistics about the heap memory usage. 
pub fn get_heap_statistics(&mut self) {
  unsafe { v8__Isolate__GetHeapStatistics(self) }
}

I get this when I try to run the function.

undefined reference to `v8__Isolate__GetHeapStatistics'
          collect2: error: ld returned 1 exit status

I build from source with V8_FROM_SOURCE=1 cargo build -vv Is there some else I need to do.

MSYS2 build failed: file too small to be an archive

Windows 10 x64 using MSYS2.

# pacman -S mingw-w64-x86_64-rust
# cargo install deno --verbose
# ... SNIP ...
     Running `C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\build\rusty_v8-994b3b26aa8c6950\build-script-build`
     Running `rustc --crate-name rusty_v8 --edition=2018 C:\Users\abcfy\.cargo\registry\src\mirrors.sjtug.sjtu.edu.cn-4f7dbcce21e258a2\rusty_v8-0.7.0\src\lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C metadata=3f85fa453429514e -C extra-filename=-3f85fa453429514e --out-dir C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\deps -L dependency=C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\deps --extern bitflags=C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\deps\libbitflags-652fbca4a9a2b259.rmeta --extern lazy_static=C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\deps\liblazy_static-a7bdb4faba14fa47.rmeta --extern libc=C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\deps\liblibc-5905678cb33dff26.rmeta --cap-lints allow -L C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\gn_out\obj -l static=rusty_v8 -l dylib=winmm -l dylib=dbghelp`
   Compiling swc_visit v0.1.0
     Running `rustc --crate-name swc_visit --edition=2018 C:\Users\abcfy\.cargo\registry\src\mirrors.sjtug.sjtu.edu.cn-4f7dbcce21e258a2\swc_visit-0.1.0\src\lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C metadata=7b29a806369777c6 -C extra-filename=-7b29a806369777c6 --out-dir C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\deps -L dependency=C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\deps --extern either=C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\deps\libeither-9d06d2f0b349566c.rmeta --extern swc_visit_macros=C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\deps\swc_visit_macros-606251da8794bb79.dll --cap-lints allow`
error: failed to add native library C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\gn_out\obj\rusty_v8.lib: file too small to be an archive

error: aborting due to previous error

error: could not compile `rusty_v8`.

Caused by:
  process didn't exit successfully: `rustc --crate-name rusty_v8 --edition=2018 C:\Users\abcfy\.cargo\registry\src\mirrors.sjtug.sjtu.edu.cn-4f7dbcce21e258a2\rusty_v8-0.7.0\src\lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C metadata=3f85fa453429514e -C extra-filename=-3f85fa453429514e --out-dir C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\deps -L dependency=C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\deps --extern bitflags=C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\deps\libbitflags-652fbca4a9a2b259.rmeta --extern lazy_static=C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\deps\liblazy_static-a7bdb4faba14fa47.rmeta --extern libc=C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\deps\liblibc-5905678cb33dff26.rmeta --cap-lints allow -L C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR\release\gn_out\obj -l static=rusty_v8 -l dylib=winmm -l dylib=dbghelp` (exit code: 1)
warning: build failed, waiting for other jobs to finish...
error: failed to compile `deno v1.2.1`, intermediate artifacts can be found at `C:\Users\abcfy\AppData\Local\Temp\cargo-installHL2kUR`

Caused by:
  build failed

build: doesn't fail when missing submodules

When trying to build with missing submodules(I didn't add --recursive to the submodule init command) it will still give build success. This is on ubuntu 18.04 not sure if this can be replicated on other platforms.

test: flaky array_buffer_with_shared_backing_store

It very sporadically fails for me locally:

---- array_buffer_with_shared_backing_store stdout ----
thread 'array_buffer_with_shared_backing_store' panicked at 'assertion failed: `(left == right)`
  left: `3`,
 right: `4`', tests/test_api.rs:395:5
note: 

Replace `download_file.py` with rust & curl

Related to #373

The curl option is there if people want to use the static binary but not use python.
This is great. I also saw the comment about moving to curl only so this is slightly related.

The python script will check locally for the lib file and skip downloading, it'd be nice if this check was done first in rust, then there's be no need for python or curl in some cases.

I can replace the python script completely if you'd like but I think I'll save that for another issue
Actually it's pretty small so I'm happy to replace it with just curl

Is it possible for rusty_v8 to rely on an external libv8(shared library)?

I'm really on rusty_v8 and have built my own js runtime with rusty_v8. But this crate is rely on v8 as a static lib (which is very big) so that my app swelled by more than 20 megabytes.

I want to reuse libv8.dylib already in my app, so is there any way to build binding.cc as static lib and link a shared libv8?

Help wanted, thx!

libdeno todo

In order to implement libdeno, we need at least these:

bert:

  • v8::TryCatch #97
  • v8::ArrayBuffer::Allocator
  • v8::ArrayBufferCreationMode::kExternalized deprecated
  • v8::Persistent
  • v8::SharedArrayBuffer::New #134
  • v8::Context::Scope

ry:

  • v8::Isolate::SetCaptureStackTraceForUncaughtExceptions #77
  • v8::Isolate::AddMessageListener #78 #89
  • v8::ArrayBuffer #106
  • v8::Module #124
  • v8::Object::CreateDataProperty (needed for import.meta) #129
  • import.meta AKA v8::Isolate::SetHostInitializeImportMetaObjectCallback #128
  • v8::Isolate::SetHostImportModuleDynamicallyCallback #136
  • v8::DeserializeInternalFieldsCallback #141
  • v8::SerializeInternalFieldsCallback #141

bartek:

  • v8::PromiseRejectMessage
  • v8::PropertyCallbackInfo
  • v8::EscapableHandleScope #113
  • v8::SnapshotCreator #122
  • v8::StartupData #122
  • v8::SnapshotCreator::FunctionCodeHandling::kKeep #122

others:

  • v8::ArrayBufferView #101
  • v8::Boolean
  • v8::Boolean::New
  • v8::Exception::TypeError
  • v8::Exception::RangeError
  • v8::Exception::ReferenceError
  • v8::Exception::SyntaxError
  • v8::Exception::Error
  • v8::Exception::GetStackTrace - needs v8::StackTrace
  • v8::Exception::CreateMessage
  • v8::Function
  • v8::FunctionCallbackInfo
  • v8::FunctionTemplate::New
  • v8::HandleScope
  • v8::Integer::New
  • v8::Isolate::CreateParams
  • v8::Isolate::GetCurrent (Bert: I don't think this is actually needed, it's just a relic from when v8 used to store the active isolate in thread local storage).
  • v8::Isolate::Scope (ditto)
  • v8::JSON::Stringify
  • v8::Locker
  • v8::MaybeLocal
  • v8::Message
  • v8::Name
  • v8::Null
  • v8::Object
  • v8::Object::New
  • v8::Promise::Resolver
  • v8::ScriptCompiler
  • v8::ScriptOrModule
  • v8::ScriptOrigin
  • v8::String
  • v8::String::NewFromUtf8
  • v8::String::Utf8Value
  • v8::Uint8Array #133
  • v8::Undefined
  • v8::V8::Initialize
  • v8::V8::InitializePlatform
  • v8::V8::SetFlagsFromCommandLine
  • v8::Value
  • v8::platform::NewDefaultPlatform

Question: Is it possible to call Rust functions?

As it says on the tin, is it possible to call rust functions from javascript code? I am planning to use rusty_v8 as an embedded javascript interpreter library. I browsed the docs and code a bit but I lack knowledge in Rust so I still wasnt sure and apologies for bothering you for this. I am looking for something like this (examples are from golang):

vm.Set("sayHello", func(call otto.FunctionCall) otto.Value {
    fmt.Printf("Hello, %s.\n", call.Argument(0).String())
    return otto.Value{}
})

vm.Run(`
    sayHello("Xyzzy");
`)

rusty_v8 not compiling when cargo build -vv

Tried to compile deno with cargo build -vv and got this.

error[E0308]: mismatched types
   --> /home/lorran/.cargo/registry/src/github.com-1ecc6299db9ec823/rusty_v8-0.3.10/src/try_catch.rs:134:5
    |
134 | /     unsafe {
135 | |       let msg = Local::from_raw(v8__TryCatch__Message(&self.0));
136 | |     }
    | |_____^ expected enum `std::option::Option`, found `()`
    |
    = note:   expected enum `std::option::Option<local::Local<'tc, data::Message>>`
            found unit type `()`

cargo 1.42.0 (86334295e 2020-01-31)
rustc 1.42.0 (b8cedc004 2020-03-09)

[Question] How to enable the output from the console API

Hi,
I've been integrating this library in a small project, for run simple scripts, all works fine so far, but the console.log(....) do not print anything, I've been digging in the code of this and of Deno to figure out how to enable that, the only API I found is V8InspectorClientImpl::console_api_message, but do not seems to me the correct entry point, could you advise me on how to enable that, or point me to the code of Deno that actually implement the console.log.

Thank you in advance
Regards

[Question] Is it a global scope at the script run?

let source = v8::String::new(scope,"let a=1;").unwrap();
v8::Script::compile(scope, source, None).unwrap().run(scope);
{
    let scope = &mut v8::HandleScope::new(scope);
    let source = v8::String::new(scope,"let a=2;").unwrap();    //Uncaught SyntaxError: Identifier 'a' has already been declared
    v8::Script::compile(scope, source, None).unwrap().run(scope);
}

let scope = &mut v8::HandleScope::new(scope);

With this, the scope is nested, and I think that it is a script run in that scope. Is it like this?

target armv5te-unknown-linux-gnu

Asset librusty_v8_debug_armv5te-unknown-linux-gnueabi.a does not exist for download - so can't build rusty_v8, when building deno.

Please consider adding this to your collection of pre-built files.

My target is; Linux lspro 5.6.0-2-marvell #1 Debian 5.6.14-1 (2020-05-23) armv5tel GNU/Linux

Implement bindings for debug-interface to use builtin console

Background: this was brought up in https://twitter.com/KevKassimo/status/1294695571446312961

It seems that we currently don't have bindings to debug-interface.h APIs, such that things like SetConsoleDelegate(to attach handlers for V8 builtin console) is not directly accessible. https://source.chromium.org/chromium/chromium/src/+/master:v8/src/debug/debug-interface.h;l=226-227;drc=459283200c38b18e13566549532ad4d7121451ef

Sample usage of this specific API could be found here: https://source.chromium.org/chromium/chromium/src/+/master:v8/src/d8/d8.cc;l=2312;drc=7e9322335ee659e70c0239fc402db7d86f4b56d6

(I still cannot experiment with anything myself yet, so maybe someone else can take the work if they want to)

support taking HeapSnapshots

To figure out performance problems with deno_tcp.ts (which are GC problems) we need to inspect what's going on on V8 heap.

This is partially related to #205 but can be implemented independently and used without full inspector support.

There's node-heapdump which implements that functionality fo node.

Grepping through node-heapdump/heapdump.cc it seems that APIs that we should port are:

  • Isolate::GetHeapProfiler
  • HeapProfiler::TakeHeapSnapshot
  • HeapSnapshot::Serialize

CC @bnoordhuis for expert advice on the subject

gn gen failure on macOS

I'm not able to build versions after the V8 upgrade to 8.2.308 (8a3c19e) from source on macOS, because of a failure in the gn gen command:

[rusty_v8 0.3.9] Downloading https://github.com/denoland/ninja_gn_binaries/archive/20200313.tar.gz... Done.
[rusty_v8 0.3.9] using Chromiums clang
[rusty_v8 0.3.9] clang_base_path /Users/chrmoritz/Documents/git/rusty_v8/target/debug/clang
[rusty_v8 0.3.9] Downloading https://commondatastorage.googleapis.com/chromium-browser-clang/Mac/clang-n344329-9284abd0-5.tgz .......... Done.
[rusty_v8 0.3.9] cargo:warning=Not using sccache
[rusty_v8 0.3.9] cargo:rustc-env=GN_OUT_DIR=/Users/chrmoritz/Documents/git/rusty_v8/target/debug/gn_out
[rusty_v8 0.3.9] running: "/Users/chrmoritz/Documents/git/rusty_v8/target/debug/ninja_gn_binaries-20200313/mac/ninja" "-C" "/Users/chrmoritz/Documents/git/rusty_v8/target/debug/gn_out" "rusty_v8"
[rusty_v8 0.3.9] ninja: Entering directory `/Users/chrmoritz/Documents/git/rusty_v8/target/debug/gn_out'
[rusty_v8 0.3.9] ninja: error: rebuilding 'build.ninja': subcommand failed
[rusty_v8 0.3.9] [1/1] Regenerating ninja files
[rusty_v8 0.3.9] FAILED: build.ninja 
[rusty_v8 0.3.9] ../build/rusty_v8-55982c004f00657d/out/gn_ninja_binaries/mac/gn --root=../../.. -q gen .
[rusty_v8 0.3.9] ERROR at //build/toolchain/mac/BUILD.gn:236:17: Unknown substitution pattern
[rusty_v8 0.3.9]       command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{framework_dirs}} {{cflags}} {{cflags_objc}} -c {{source}} -o {{output}}"
[rusty_v8 0.3.9]                 ^-----------------------------------------------------------------------------------------------------------------------------
[rusty_v8 0.3.9] Found a {{ at offset 106 and did not find a known substitution following it.
[rusty_v8 0.3.9] See //build/toolchain/mac/BUILD.gn:484:1: whence it was called.
[rusty_v8 0.3.9] mac_toolchain("clang_arm") {
[rusty_v8 0.3.9] ^---------------------------
[rusty_v8 0.3.9] See //v8/gni/v8.gni:266:3: which caused the file to be included.
[rusty_v8 0.3.9]   static_library(target_name) {
[rusty_v8 0.3.9]   ^----------------------------
[rusty_v8 0.3.9] thread 'main' panicked at '
[rusty_v8 0.3.9] command did not execute successfully, got: exit code: 1
[rusty_v8 0.3.9] 
[rusty_v8 0.3.9] build script failed, must exit now', /Users/chrmoritz/.cargo/registry/src/github.com-1ecc6299db9ec823/cargo_gn-0.0.15/src/lib.rs:203:3
[rusty_v8 0.3.9] note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
error: failed to run custom build command for `rusty_v8 v0.3.9 (/Users/chrmoritz/Documents/git/rusty_v8)`

Relevant chromium/build commit, which has introduced the {{framework_dirs}} at offset 70, which it's complaining about: denoland/chromium_build@62dad5c#diff-0448a2a05f9be285643f46f44cc31272.

This also blocks us in Homebrew from upgrading deno past 0.36.0 (Refs: Homebrew/homebrew-core#52095)

Segfault on Mac

Hi, I just found this awesome crate and am trying to use it as an interpreter for a chatbot.

Unfortunately I haven't gotten very far. After installing rusty_v8 I run into a segfault immediately when importing the crate (use rusty_v8 as v8;).

Running with rust-lldb, here's the problematic section:

# rust-lldb -- target/debug/stov_bot
(lldb) command script import "/Users/stovoy/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/etc/lldb_rust_formatters.py"
(lldb) type summary add --no-value --python-function lldb_rust_formatters.print_val -x ".*" --category Rust                                                            (lldb) type category enable Rust
(lldb) target create "target/debug/stov_bot"
Current executable set to 'target/debug/stov_bot' (x86_64).
(lldb) r
Process 62008 launched: '/Users/stovoy/src/StovBot/target/debug/stov_bot' (x86_64)
2019-12-29 22:41:21.437612-0500 stov_bot[62008:1049310] flock failed to lock maps file: errno = 35
Process 62008 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
    frame #0: 0x000000010117bac5 stov_bot`std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__is_long(this=&0x5f5f5f5f5f5f5f38) co
nst at string:1421:22
   1418
   1419     _LIBCPP_INLINE_VISIBILITY
   1420     bool __is_long() const _NOEXCEPT
-> 1421         {return bool(__r_.first().__s.__size_ & __short_mask);}                                                                                                   1422
   1423 #if _LIBCPP_DEBUG_LEVEL >= 2
   1424
Target 0: (stov_bot) stopped.

Here's my version of Rust:

# rustup show
Default host: x86_64-apple-darwin
rustup home:  /Users/stovoy/.rustup

installed toolchains
--------------------

stable-x86_64-apple-darwin (default)
nightly-x86_64-apple-darwin

active toolchain
----------------

stable-x86_64-apple-darwin (default)
rustc 1.40.0 (73528e339 2019-12-16)

I'm on MacOSX Catalina. It segfaults the same way in both debug and release builds.

Let me know what other info I could provide to help troubleshoot this. Thanks!

Configurable download url

Hello.
I want to use a mirror server for downloading static libraries in build.rs because downloading files from GitHub is extremely slow in my home.

Could you add an option to change download url like this?

--- a/build.rs
+++ b/build.rs
@@ -168,7 +168,8 @@ fn download_ninja_gn_binaries() {
 }

 fn static_lib_url() -> (String, String) {
-  let base = "https://github.com/denoland/rusty_v8/releases/download";
+  let default_base = "https://github.com/denoland/rusty_v8/releases/download";
+  let base = env::var("RUSTY_V8_MIRROR").unwrap_or_else(|_| default_base.into());
   let version = env::var("CARGO_PKG_VERSION").unwrap();
   let target = env::var("TARGET").unwrap();
   if cfg!(target_os = "windows") {

Add design documentation for scopes

Although the end is in sight, the design is still a bit in flux.

The general idea is that the various scope classes mediate access to the v8 Isolate and the various items on its heap (Local/Global handles, return/escape slots, etc.). At any point in time there exists only one scope object that is directly accessible, which guarantees that all interactions with the Isolate are safe.

A Scope as such is not a trait (there is currently an internal ScopeDefinition trait but that's only there to make implementation easier).

Rather, there are a number of traits that are implemented for the scopes they're applicable to, you've probably seen most of them already. The InIsolate which gives access to &mut Isolate is implemented for all scopes, ToLocal (I might rename that) is implemented for all Scopes in which new Local handles can be created and it sets the appropriate lifetime on them. InContext means that a context has been entered (I'll make sure they have a get_context() method), etc.

Furthermore, many callbacks will receive receive an appropriate Scope object as their first argument, which 'encodes' the the state the isolate is in when the callback is called. E.g. a FunctionCallbackScope implements InIsolate + InContext + (there is an active context) and ToLocal (it acts as a handlescope). HostImportModuleDynamicallyScope would also implement InIsolate + InContext plus EscapeLocal (it doesn't act like a HandleScope, but it lets you safely escape one MaybeLocal which is returned to the caller.

In a nutshell, that's it.
Open TODOs are:

Add these automatic scopes to more callbacks (in progress) and get rid of the necessity to import the MapFnTo trait.
Fully integrate TryCatch blocks into the scope system (currently a TryCatch works like a scope internally but it's not integrated).
Add methods to some on some of the scopes like get_context() for ContextScope.
Rename/reorganize/document.

test: ScriptOrigin makes context_from_object_template crash

At commit 683aa2b.

diff --git a/tests/test_api.rs b/tests/test_api.rs
index aabbac7..05adf01 100644
--- a/tests/test_api.rs
+++ b/tests/test_api.rs
@@ -379,7 +379,9 @@ fn eval<'sc>(
   let mut hs = v8::EscapableHandleScope::new(scope);
   let scope = hs.enter();
   let source = v8_str(scope, code);
-  let mut script = v8::Script::compile(scope, context, source, None).unwrap();
+  let origin = mock_script_origin(scope, "test.js");
+  let mut script =
+    v8::Script::compile(scope, context, source, Some(&origin)).unwrap();
   let r = script.run(scope, context);
   r.map(|v| scope.escape(v))
 }
$ cargo test context_from_object_template
    Finished test [unoptimized + debuginfo] target(s) in 0.03s
     Running target/debug/deps/rusty_v8-98486895b0660b61

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 6 filtered out

     Running target/debug/deps/test_api-e710bec475054917

running 1 test


#
# Fatal error in ../../../v8/src/execution/isolate.h, line 546
# Debug check failed: (isolate) != nullptr.
#
#
#
#FailureMessage Object: 0x7f9635787b50
==== C stack trace ===============================

    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x13642a3) [0x55a7c3bef2a3]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0xfa486d) [0x55a7c382f86d]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x839473) [0x55a7c30c4473]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x839175) [0x55a7c30c4175]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x198fc4) [0x55a7c2a23fc4]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x1a7422) [0x55a7c2a32422]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0xd43cd) [0x55a7c295f3cd]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x133b0f) [0x55a7c29beb0f]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x116072) [0x55a7c29a1072]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x13536a) [0x55a7c29c036a]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x13161e) [0x55a7c29bc61e]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x142bff) [0x55a7c29cdbff]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x1733357) [0x55a7c3fbe357]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x15d95b) [0x55a7c29e895b]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x136fe6) [0x55a7c29c1fe6]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x13b696) [0x55a7c29c6696]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x1733357) [0x55a7c3fbe357]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x13c1d6) [0x55a7c29c71d6]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x1720e7f) [0x55a7c3fabe7f]
    /home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917(+0x17328c0) [0x55a7c3fbd8c0]
    /lib/x86_64-linux-gnu/libpthread.so.0(+0x9669) [0x7f9635af2669]
    /lib/x86_64-linux-gnu/libc.so.6(clone+0x43) [0x7f9635a00323]
error: test failed, to rerun pass '--test test_api'

Caused by:
  process didn't exit successfully: `/home/bnoordhuis/src/rusty_v8/target/debug/deps/test_api-e710bec475054917 context_from_object_template` (signal: 4, SIGILL: illegal instruction)

I'll look into this if no one beats me to it.

Support for Externals

Currently it doesn't appear that we can use v8::External's from Rust.

I would like to bind a Rust struct to the VM and the way I would do that in C++ would be to store the pointer using obj->SetInternalField(0, External::New(myobjectpointer)); and then back again.

It would be awesome if we could Box up a struct and put it in an External by it's pointer. I'm not sure if there's a way to do that in the reverse without using unsafe but I would be happy with that as I know what types I am using.

The last thing to keep in mind is that a way to provide a finalizer to drop the box and free the memory should be possible in order to avoid holding onto memory forever. From a quick search it looks like PersistentBase::SetWeak is used to cleanup memory for bound C++ classes in Node.js. I'm not sure how this would translate to Rust.

Build error on target `aarch64-linux-android`

I'm building deno for termux, and meting these errors when building rusty_v8

cargo build --target aarch64-linux-android --verbose
...
   Compiling rusty_v8 v0.6.0 (/root/.cargo/registry/src/mirrors.bfsu.edu.cn-4c6e9dcaa6bd74e7/rusty_v
8-0.6.0)
     Running `rustc --crate-name rusty_v8 --edition=2018 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C metadata=1b6bbfcc37da8f87 -C extra-filename=-1b6bbfcc37da8f87 --out-dir /root/.cargo/registry/src/mirrors.bfsu.edu.cn-4c6e9dcaa6bd74e7/rusty_v8-0.6.0/target/aarch64-linux-android/release/deps --target aarch64-linux-android -C linker=aarch64-linux-android29-clang -L dependency=/root/.cargo/registry/src/mirrors.bfsu.edu.cn-4c6e9dcaa6bd74e7/rusty_v8-0.6.0/target/aarch64-linux-android/release/deps -L dependency=/root/.cargo/registry/src/mirrors.bfsu.edu.cn-4c6e9dcaa6bd74e7/rusty_v8-0.6.0/target/release/deps --extern bitflags=/root/.cargo/registry/src/mirrors.bfsu.edu.cn-4c6e9dcaa6bd74e7/rusty_v8-0.6.0/target/aarch64-linux-android/release/deps/libbitflags-cad15626e69d129f.rmeta --extern lazy_static=/root/.cargo/registry/src/mirrors.bfsu.edu.cn-4c6e9dcaa6bd74e7/rusty_v8-0.6.0/target/aarch64-linux-android/release/deps/liblazy_static-dc5582728683eb4a.rmeta --extern libc=/root/.cargo/registry/src/mirrors.bfsu.edu.cn-4c6e9dcaa6bd74e7/rusty_v8-0.6.0/target/aarch64-linux-android/release/deps/liblibc-65da228d2306fcd8.rmeta -L /root/.cargo/registry/src/mirrors.bfsu.edu.cn-4c6e9dcaa6bd74e7/rusty_v8-0.6.0/target/a
arch64-linux-android/release/gn_out/obj -l static=rusty_v8`
error: failed to add native library /root/.cargo/registry/src/mirrors.bfsu.edu.cn-4c6e9dcaa6bd74e7/r
usty_v8-0.6.0/target/aarch64-linux-android/release/gn_out/obj/librusty_v8.a: File too small to be an
 archive

error: aborting due to previous error

error: could not compile `rusty_v8`.

Caused by:
  process didn't exit successfully: `rustc --crate-name rusty_v8 --edition=2018 src/lib.rs --error-f
ormat=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C opt-lev
el=3 -C metadata=1b6bbfcc37da8f87 -C extra-filename=-1b6bbfcc37da8f87 --out-dir /root/.cargo/registr
y/src/mirrors.bfsu.edu.cn-4c6e9dcaa6bd74e7/rusty_v8-0.6.0/target/aarch64-linux-android/release/deps
--target aarch64-linux-android -C linker=aarch64-linux-android29-clang -L dependency=/root/.cargo/re
gistry/src/mirrors.bfsu.edu.cn-4c6e9dcaa6bd74e7/rusty_v8-0.6.0/target/aarch64-linux-android/release/
deps -L dependency=/root/.cargo/registry/src/mirrors.bfsu.edu.cn-4c6e9dcaa6bd74e7/rusty_v8-0.6.0/tar
get/release/deps --extern bitflags=/root/.cargo/registry/src/mirrors.bfsu.edu.cn-4c6e9dcaa6bd74e7/ru
sty_v8-0.6.0/target/aarch64-linux-android/release/deps/libbitflags-cad15626e69d129f.rmeta --extern l
azy_static=/root/.cargo/registry/src/mirrors.bfsu.edu.cn-4c6e9dcaa6bd74e7/rusty_v8-0.6.0/target/aarc
h64-linux-android/release/deps/liblazy_static-dc5582728683eb4a.rmeta --extern libc=/root/.cargo/regi
stry/src/mirrors.bfsu.edu.cn-4c6e9dcaa6bd74e7/rusty_v8-0.6.0/target/aarch64-linux-android/release/de
ps/liblibc-65da228d2306fcd8.rmeta -L /root/.cargo/registry/src/mirrors.bfsu.edu.cn-4c6e9dcaa6bd74e7/
rusty_v8-0.6.0/target/aarch64-linux-android/release/gn_out/obj -l static=rusty_v8` (exit code: 1)

Get link errors when upgrading rusty_v8

When I use rusty_v8 as a dependency and it gets upgrade, it doesn't seem to use the new librusty_v8.a because I get link errors if the ABI has changed.

For example, a recent upgrade of deno_core resulted in this error

  = note: Undefined symbols for architecture x86_64:
            "_v8__Isolate__CreateParams__SIZEOF", referenced from:
                _$LT$rusty_v8..isolate_create_params..raw..CreateParams$u20$as$u20$core..default..Default$GT$::default::h57323b37433ca0e9 in librusty_v8-2afbb5eb4a6bf179.rlib(rusty_v8-2afbb5eb4a6bf179.rusty_v8.8w61v7cb-cgu.3.rcgu.o)
            "_v8__Isolate__CreateParams__CONSTRUCT", referenced from:
                _$LT$rusty_v8..isolate_create_params..raw..CreateParams$u20$as$u20$core..default..Default$GT$::default::h57323b37433ca0e9 in librusty_v8-2afbb5eb4a6bf179.rlib(rusty_v8-2afbb5eb4a6bf179.rusty_v8.8w61v7cb-cgu.3.rcgu.o)
          ld: symbol(s) not found for architecture x86_64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

Fix function callback lifetimes

See commit in my fork for my personal fix: Protryon@0a4b6f2

Issue is that the implied lifetimes for the callback type are distinct, which does not mirror the presumably intended behavior, requiring unsafe code to overcome in third party code.

Broken binary on 64-bit Windows

I might be doing something wrong, but it seems binding.cc gets built for x64, while the Rust code gets built for x86. This causes linker errors when this crate is used: denoland/deno#3716

Steps:

  1. Clone the repo and run cargo build -vv.
  2. Open target/debug/librusty_v8.rlib in 7-Zip, extract rusty_v8/binding.obj and one of the Rust .o files, compare their dumpbin /headers outputs.

Expected: same "machine" values.

Actual: Rust object files have 14C machine (x86), binding.obj has 8664 machine (x64).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.