Finalize the bindings for now. These should be enough to work with.
This commit is contained in:
parent
20262faa13
commit
79eed41f2e
2 changed files with 37 additions and 23 deletions
|
@ -1,13 +1,13 @@
|
|||
[package]
|
||||
name = "dukbind"
|
||||
version = "0.0.1"
|
||||
version = "0.0.2"
|
||||
authors = ["envis10n <envis10n@protonmail.com>"]
|
||||
edition = "2018"
|
||||
build = "build.rs"
|
||||
exclude = ["duktape/examples/**/*", "duktape/src-separate/**/*"]
|
||||
repository = "https://gitlab.com/envis10n/dukbind"
|
||||
readme = "README.md"
|
||||
keyword = ["duktape", "dukbind", "js", "javascript"]
|
||||
keywords = ["duktape", "dukbind", "js", "javascript"]
|
||||
license = "Unlicense"
|
||||
description = "Raw duktape bindings for rust."
|
||||
|
||||
|
|
42
src/lib.rs
42
src/lib.rs
|
@ -1,12 +1,34 @@
|
|||
#![allow(non_upper_case_globals)]
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/duktape_bindings.rs"));
|
||||
use std::ffi;
|
||||
pub unsafe fn duk_create_heap_default() -> *mut duk_context {
|
||||
use std::ptr::null_mut;
|
||||
let ctx = duk_create_heap(None, None, None, null_mut(), None);
|
||||
unsafe extern "C" fn printc(c: *mut duk_context) -> i32 {
|
||||
let cstr = ffi::CStr::from_ptr(duk_to_string(c, 0));
|
||||
println!("{0}", cstr.to_str().unwrap());
|
||||
0
|
||||
}
|
||||
duk_push_global_function(ctx, "print", Some(printc), 1);
|
||||
ctx
|
||||
}
|
||||
pub unsafe fn duk_eval_string(ctx: *mut duk_context, code: &str) -> u32 {
|
||||
let filename = "input";
|
||||
duk_push_lstring(ctx, filename.as_ptr() as *const i8,
|
||||
filename.len() as duk_size_t);
|
||||
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
|
||||
}
|
||||
pub unsafe fn duk_push_global_function(ctx: *mut duk_context, name: &str, f: duk_c_function, args: i32) -> bool {
|
||||
duk_push_c_function(ctx, f, args);
|
||||
duk_put_global_lstring(ctx, name.as_ptr() as *const i8, name.len() as duk_size_t) == 0
|
||||
}
|
||||
|
||||
pub mod glue {
|
||||
use super::*;
|
||||
extern "C" {
|
||||
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
|
||||
|
@ -14,29 +36,21 @@ pub mod glue {
|
|||
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);
|
||||
let ctx = duk_create_heap_default();
|
||||
|
||||
// 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;
|
||||
let result = duk_eval_string(ctx, code);
|
||||
assert_eq!(DUK_EXEC_SUCCESS, result);
|
||||
|
||||
// Get the result and make sure it's correct.
|
||||
|
|
Loading…
Reference in a new issue