Skip to content

Commit

Permalink
Fix some Rust issues. (#51)
Browse files Browse the repository at this point in the history
* Update .gitignore

* Update README.md

* Fix a few rust issues.

1. Cosmetically changes the test structure and the test function names
   in the lib.rs to avoid duplicate words and group the similar tests or
   the tests of the same scope into modules.
2. Add a test for the README.md file code.
3. Derive the Debug trait for as many structures as possible.
4. Add more convenient methods to the V8LocalString.
5. Split the V8IsolateScope into two objects, one of which follows the
   Memento design pattern, allowing to temporary store the content of
   V8IsolateScope and restore from it. Also, reduces the amount of
   responsibility the V8IsolateScope has and groups the *Scope objects into
   one separate structure, which is (or might be) easier to work with.

* Add the minimal .clangd file to help with C++ code.

* Remove the commented code

* Remove the commented code

* Hide IsolateScope::new_dummy

* Remove unused isolate scope facilities

* Hide some context scope methods and use NonNull

* Make the V8LocalString API safer

* Add isolate ID.

Having a unique identification number for each isolate helps tracking
down isolates and comparing them for equality.

* Check for the isolate id for V8PersistedScript.

Adds a check to make sure it is impossible to create a V8LocalScript
object from the V8PersistedScript object if the isolates differ.

* Hide get_current_context_scope under pub(crate).

* Hide V8Isolate::get_raw and get_id

* Perform a minor refactoring of v8_module
  • Loading branch information
iddm authored Sep 4, 2023
1 parent fc6f616 commit 6f20f22
Show file tree
Hide file tree
Showing 22 changed files with 1,111 additions and 888 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ Cargo.lock
.vscode/*
*.o
*.a

# Debugging history.
.gdb_history

# Developer tools
.clangd*
50 changes: 24 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,50 @@
# v8-rs
Rust wrapper for v8
Rust wrapper for the [Google V8 JavaScript Engine](https://chromium.googlesource.com/v8/v8).

Example:
## Example:

```rust
use crate::v8::*;
use v8_rs::v8::*;

// initialized v8
// Initialise the V8 engine:
v8_init_platform(1, Some("--expose-gc")).unwrap();
v8_init();

// Create a new isolate
// Create a new isolate:
let isolate = isolate::V8Isolate::new();

// Create a new isolate scope
let _h_scope = isolate.new_handlers_scope();

// Enter the isolate
// Enter the isolate created:
let i_scope = isolate.enter();

// Create the code string object
let code_str = isolate.new_string("1+1");
// Create the code string object:
let code_str = i_scope.new_string("1+1");

// Create a JS context for code invocation.
// Create a JS execution context for code invocation:""
let ctx = i_scope.new_context(None);

// Enter the created context
let ctx_scope = ctx.enter();
// Enter the created execution context:
let ctx_scope = ctx.enter(&i_scope);

// Compile the code
// Compile the code:
let script = ctx_scope.compile(&code_str).unwrap();

// Run the code
// Run the compiled code:
let res = script.run(&ctx_scope).unwrap();

// Get the result
let res_utf8 = res.to_utf8(&isolate).unwrap();
// Get the result:
let res_utf8 = res.to_utf8().unwrap();
assert_eq!(res_utf8.as_str(), "2");
```

## Build Options

Usually, just adding the crate as a dependency in your project will be enough. That said it is possible to change the following build option using evironment variables.

* V8_VERSION - will change the default V8 version to use.
* V8_UPDATE_HEADERS - will update the V8 headers according to the set version, allow to also set the following options:
* V8_HEADERS_PATH - control where to download the headers zip file, default `v8_c_api/libv8.include.zip`.
* V8_FORCE_DOWNLOAD_V8_HEADERS - download the V8 headers zip file even if it is already exists.
* V8_HEADERS_URL - url from where to download the V8 headers zip file.
* V8_MONOLITH_PATH - control where to download the V8 monolith, default `v8_c_api/libv8_monolith.a`
* V8_FORCE_DOWNLOAD_V8_MONOLITH - download the V8 monolith even if it is already exists.
* V8_MONOLITH_URL - url from where to download the V8 monolith file.
* `V8_VERSION` - will change the default V8 version to use.
* `V8_UPDATE_HEADERS` - will update the V8 headers according to the set version, allow to also set the following options:
* `V8_HEADERS_PATH` - control where to download the headers zip file, default `v8_c_api/libv8.include.zip`.
* `V8_FORCE_DOWNLOAD_V8_HEADERS` - download the V8 headers zip file even if it is already exists.
* `V8_HEADERS_URL` - url from where to download the V8 headers zip file.
* `V8_MONOLITH_PATH` - control where to download the V8 monolith, default `v8_c_api/libv8_monolith.a`
* `V8_FORCE_DOWNLOAD_V8_MONOLITH` - download the V8 monolith even if it is already exists.
* `V8_MONOLITH_URL` - url from where to download the V8 monolith file.
Loading

0 comments on commit 6f20f22

Please sign in to comment.