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]
|
[package]
|
||||||
name = "dukbind"
|
name = "dukbind"
|
||||||
version = "0.0.1"
|
version = "0.0.2"
|
||||||
authors = ["envis10n <envis10n@protonmail.com>"]
|
authors = ["envis10n <envis10n@protonmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
build = "build.rs"
|
build = "build.rs"
|
||||||
exclude = ["duktape/examples/**/*", "duktape/src-separate/**/*"]
|
exclude = ["duktape/examples/**/*", "duktape/src-separate/**/*"]
|
||||||
repository = "https://gitlab.com/envis10n/dukbind"
|
repository = "https://gitlab.com/envis10n/dukbind"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
keyword = ["duktape", "dukbind", "js", "javascript"]
|
keywords = ["duktape", "dukbind", "js", "javascript"]
|
||||||
license = "Unlicense"
|
license = "Unlicense"
|
||||||
description = "Raw duktape bindings for rust."
|
description = "Raw duktape bindings for rust."
|
||||||
|
|
||||||
|
|
56
src/lib.rs
56
src/lib.rs
|
@ -1,42 +1,56 @@
|
||||||
#![allow(non_upper_case_globals)]
|
#![allow(non_upper_case_globals)]
|
||||||
#![allow(non_camel_case_types)]
|
#![allow(non_camel_case_types)]
|
||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
include!(concat!(env!("OUT_DIR"), "/duktape_bindings.rs"));
|
include!(concat!(env!("OUT_DIR"), "/duktape_bindings.rs"));
|
||||||
|
use std::ffi;
|
||||||
pub mod glue {
|
pub unsafe fn duk_create_heap_default() -> *mut duk_context {
|
||||||
use super::*;
|
use std::ptr::null_mut;
|
||||||
extern "C" {
|
let ctx = duk_create_heap(None, None, None, null_mut(), None);
|
||||||
/// A wrapper around duk_push_error_object, which relies on varargs in
|
unsafe extern "C" fn printc(c: *mut duk_context) -> i32 {
|
||||||
/// the original API.
|
let cstr = ffi::CStr::from_ptr(duk_to_string(c, 0));
|
||||||
/// NOTE: Pulled from duktape crate duktape_sys/src/glue.rs
|
println!("{0}", cstr.to_str().unwrap());
|
||||||
pub fn duk_push_error_object_string(
|
0
|
||||||
ctx: *mut duk_context, err_code: duk_errcode_t,
|
|
||||||
filename: *const i8, line: duk_int_t,
|
|
||||||
message: *const i8) -> duk_idx_t;
|
|
||||||
}
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_eval() {
|
fn test_eval() {
|
||||||
use std::ptr::null_mut;
|
|
||||||
use super::*;
|
use super::*;
|
||||||
unsafe {
|
unsafe {
|
||||||
// Pulled from duktape crate duktape_sys/src/lib.rs | Modified to compile.
|
// Pulled from duktape crate duktape_sys/src/lib.rs | Modified to compile.
|
||||||
// Create a heap.
|
// 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.
|
// Run a short code snippet.
|
||||||
let code = "2+3";
|
let code = "2+3";
|
||||||
let filename = "input";
|
let result = duk_eval_string(ctx, code);
|
||||||
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);
|
assert_eq!(DUK_EXEC_SUCCESS, result);
|
||||||
|
|
||||||
// Get the result and make sure it's correct.
|
// Get the result and make sure it's correct.
|
||||||
|
|
Loading…
Reference in a new issue