Initial push
This commit is contained in:
parent
2c5d2a7b5e
commit
c23b1864f3
6 changed files with 91 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
/target
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
[submodule "duktape"]
|
||||
path = duktape
|
||||
url = https://github.com/svaarala/duktape-releases.git
|
12
Cargo.toml
Normal file
12
Cargo.toml
Normal file
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "dukbind"
|
||||
version = "0.1.0"
|
||||
authors = ["envis10n <envis10n@protonmail.com>"]
|
||||
edition = "2018"
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
|
||||
[build-dependencies]
|
||||
cc = "*"
|
||||
bindgen = "*"
|
22
build.rs
Normal file
22
build.rs
Normal file
|
@ -0,0 +1,22 @@
|
|||
extern crate bindgen;
|
||||
extern crate cc;
|
||||
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
cc::Build::new()
|
||||
.file("duktape/src/duktape.c")
|
||||
.include("duktape/src")
|
||||
.compile("duktape");
|
||||
|
||||
let bindings = bindgen::Builder::default()
|
||||
.header("duktape/src/duktape.h")
|
||||
.generate()
|
||||
.expect("Unable to generate bindings");
|
||||
|
||||
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
|
||||
bindings
|
||||
.write_to_file(out_path.join("duktape_bindings.rs"))
|
||||
.expect("Couldn't write bindings!");
|
||||
}
|
1
duktape
Submodule
1
duktape
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit 6a17dc066fb4d2ffa33d593f0cc4ada18885fde7
|
50
src/lib.rs
Normal file
50
src/lib.rs
Normal file
|
@ -0,0 +1,50 @@
|
|||
#![allow(non_upper_case_globals)]
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/duktape_bindings.rs"));
|
||||
|
||||
pub mod glue {
|
||||
use super::*;
|
||||
extern "C" {
|
||||
/// A wrapper around duk_push_error_object, which relies on varargs in
|
||||
/// the original API.
|
||||
/// NOTE: Pulled from duktape crate duktape_sys/src/glue.rs
|
||||
pub fn duk_push_error_object_string(
|
||||
ctx: *mut duk_context, err_code: duk_errcode_t,
|
||||
filename: *const i8, line: duk_int_t,
|
||||
message: *const i8) -> duk_idx_t;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn test_eval() {
|
||||
use std::ptr::null_mut;
|
||||
use super::*;
|
||||
unsafe {
|
||||
// Pulled from duktape crate duktape_sys/src/lib.rs | Modified to compile.
|
||||
// Create a heap.
|
||||
let ctx = duk_create_heap(None, None, None, null_mut(), None);
|
||||
|
||||
// Run a short code snippet.
|
||||
let code = "2+3";
|
||||
let filename = "input";
|
||||
duk_push_lstring(ctx, filename.as_ptr() as *const i8,
|
||||
filename.len() as duk_size_t);
|
||||
let result = duk_eval_raw(ctx, code.as_ptr() as *const i8,
|
||||
code.len() as duk_size_t,
|
||||
DUK_COMPILE_EVAL | DUK_COMPILE_NOSOURCE |
|
||||
DUK_COMPILE_SAFE) as u32;
|
||||
assert_eq!(DUK_EXEC_SUCCESS, result);
|
||||
|
||||
// Get the result and make sure it's correct.
|
||||
assert_eq!(1, duk_is_number(ctx, -1));
|
||||
assert_eq!(5.0, duk_get_number(ctx, -1));
|
||||
duk_pop(ctx);
|
||||
|
||||
duk_destroy_heap(ctx);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue