mirror of
https://github.com/denoland/rusty_v8.git
synced 2024-11-21 15:04:33 -05:00
add hello world example (#142)
This commit is contained in:
parent
29fa5388f5
commit
f2cc688439
2 changed files with 40 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,4 +2,5 @@
|
||||||
**/*.orig
|
**/*.orig
|
||||||
|
|
||||||
/.vscode/
|
/.vscode/
|
||||||
|
/.idea/
|
||||||
/target/
|
/target/
|
||||||
|
|
39
src/lib.rs
39
src/lib.rs
|
@ -1,5 +1,44 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
|
//! # Hello World Example
|
||||||
|
//!
|
||||||
|
//! ```
|
||||||
|
//! use rusty_v8::{Isolate, V8, HandleScope, Script, Context, Locker, Local};
|
||||||
|
//! use rusty_v8::{platform, new_default_allocator};
|
||||||
|
//!
|
||||||
|
//! let platform = platform::new_default_platform();
|
||||||
|
//! V8::initialize_platform(platform);
|
||||||
|
//! V8::initialize();
|
||||||
|
//!
|
||||||
|
//! let mut create_params = Isolate::create_params();
|
||||||
|
//! create_params.set_array_buffer_allocator(new_default_allocator());
|
||||||
|
//! let isolate = Isolate::new(create_params);
|
||||||
|
//! let mut locker = Locker::new(&isolate);
|
||||||
|
//!
|
||||||
|
//! {
|
||||||
|
//! let mut handle_scope = HandleScope::new(&mut locker);
|
||||||
|
//! let scope = handle_scope.enter();
|
||||||
|
//!
|
||||||
|
//! let mut context = Context::new(scope);
|
||||||
|
//! context.enter();
|
||||||
|
//! let code = rusty_v8::String::new(scope, "'Hello World!'").unwrap();
|
||||||
|
//! code.to_rust_string_lossy(scope);
|
||||||
|
//! let mut script = Script::compile(scope, context, code, None).unwrap();
|
||||||
|
//! let result = script.run(scope, context).unwrap();
|
||||||
|
//! let result: Local<rusty_v8::String> = unsafe { std::mem::transmute_copy(&result) };
|
||||||
|
//!
|
||||||
|
//! let str = result.to_rust_string_lossy(scope);
|
||||||
|
//!
|
||||||
|
//! println!("{}", str);
|
||||||
|
//!
|
||||||
|
//! context.exit();
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! drop(locker);
|
||||||
|
//!
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
|
||||||
#![allow(clippy::missing_safety_doc)]
|
#![allow(clippy::missing_safety_doc)]
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue