Skip to content

Instantly share code, notes, and snippets.

@sanxiyn
Created March 22, 2013 08:26
Show Gist options
  • Select an option

  • Save sanxiyn/5219751 to your computer and use it in GitHub Desktop.

Select an option

Save sanxiyn/5219751 to your computer and use it in GitHub Desktop.
JSVal and ABI
// See
// https://github.com/mozilla-servo/rust-mozjs/blob/master/jsapi.rs
// https://github.com/mozilla-servo/rust-mozjs/blob/master/glue.rs
use core::libc::*;
type JSVal = u64;
#[link_args = "-L. -ljsval"]
#[link_name = "jsval"]
extern {
fn JSVAL_TO_INT32(++v: JSVal) -> int32_t;
fn INT32_TO_JSVAL(++i: int32_t) -> JSVal;
}
fn main() {
let i1 = 1;
let v = unsafe { INT32_TO_JSVAL(i1) };
let i2 = unsafe { JSVAL_TO_INT32(v) };
fail_unless!(i1 == i2);
}
// See
// https://github.com/mozilla-servo/mozjs/blob/master/js/src/jsval.h
// https://github.com/mozilla-servo/mozjs/blob/master/js/src/jsapi.h
// https://github.com/mozilla-servo/rust-mozjs/blob/master/jsglue.cpp
#include <stdint.h>
typedef union jsval {
uint64_t asBits;
struct {
int32_t payload;
uint32_t tag;
} s;
} jsval;
int32_t JSVAL_TO_INT32(jsval v) {
return v.s.payload;
}
jsval INT32_TO_JSVAL(int32_t i) {
jsval v;
v.s.tag = 0;
v.s.payload = i;
return v;
}
ifndef ARM
run: libjsval.so js.x64
LD_LIBRARY_PATH=. ./js.x64
else
run: libjsval.so js.arm
endif
clean:
rm -f *.ll *.s *.so *.x64 *.arm
ifndef ARM
%.o: %.c
gcc -fPIC -c $< -o $@
lib%.so: %.o
gcc -shared $< -o $@
else
%.o: %.c
arm-linux-androideabi-gcc -fPIC -c $< -o $@
lib%.so: %.o
arm-linux-androideabi-gcc -shared $< -o $@
endif
%.x64: %.rs
rustc --target x86_64-unknown-linux-gnu $< -o $@
%.arm: %.rs
rustc --target arm-linux-androideabi --android-cross-path /opt/ndk $< -o $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment