mirror of
https://github.com/servo/servo.git
synced 2025-07-24 15:50:21 +01:00
refactor to have some global fns in JS
This commit is contained in:
parent
6c5ed2e30d
commit
adc6d3701d
5 changed files with 82 additions and 42 deletions
|
@ -6,7 +6,9 @@
|
|||
# 48000
|
||||
|
||||
ETCDIR=$(dirname $0)
|
||||
INCDIR=${ETCDIR}/../mozjs/dist/include
|
||||
JSDIR=${ETCDIR}/../../build/src/mozjs/dist/
|
||||
INCDIR=${JSDIR}/include
|
||||
LIBDIR=${JSDIR}/lib
|
||||
|
||||
echo > jsdefine.c
|
||||
echo '#include "jsapi.h"' >> jsdefine.c
|
||||
|
@ -17,5 +19,5 @@ echo ' "'"$1"'\n"', >> jsdefine.c
|
|||
echo " $2);" >> jsdefine.c
|
||||
echo '}' >> jsdefine.c
|
||||
|
||||
gcc -I ${INCDIR} jsdefine.c -o jsdefine.exe
|
||||
g++ -I ${INCDIR} jsdefine.c -o jsdefine.exe
|
||||
./jsdefine.exe
|
||||
|
|
|
@ -63,8 +63,8 @@ fn content(to_layout: chan<layout::msg>) -> chan<msg> {
|
|||
}
|
||||
result::ok(bytes) {
|
||||
let cx = rt.cx();
|
||||
cx.new_global(jsglobal::global_class()).chain { |glob|
|
||||
cx.evaluate_script(glob, bytes, filename, 1u)
|
||||
cx.new_compartment(jsglobal::global_class).chain { |comp|
|
||||
cx.evaluate_script(comp.global_obj, bytes, filename, 1u)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,11 +3,11 @@ import jsapi::bindgen::*;
|
|||
import ptr::{null, addr_of};
|
||||
import result::{result, ok, err, extensions};
|
||||
import libc::c_char;
|
||||
import name_pool::{name_pool, methods};
|
||||
|
||||
export rt;
|
||||
export methods;
|
||||
export cx;
|
||||
export named_class;
|
||||
export jsobj;
|
||||
|
||||
const default_heapsize: u32 = 8_u32 * 1024_u32 * 1024_u32;
|
||||
|
@ -18,9 +18,9 @@ fn result(n: JSBool) -> result<(),()> {
|
|||
if n != ERR {ok(())} else {err(())}
|
||||
}
|
||||
|
||||
type named_class = @{
|
||||
name: str,
|
||||
jsclass: JSClass
|
||||
type named_functions = @{
|
||||
names: [str],
|
||||
funcs: [JSFunctionSpec]
|
||||
};
|
||||
|
||||
// ___________________________________________________________________________
|
||||
|
@ -58,14 +58,18 @@ impl methods for cx {
|
|||
jsobj
|
||||
}
|
||||
|
||||
fn new_global(globcls: named_class) -> result<jsobj,()> {
|
||||
fn new_compartment(globclsfn: fn(name_pool) -> JSClass) -> result<compartment,()> {
|
||||
let np = name_pool();
|
||||
let globcls = @globclsfn(np);
|
||||
let globobj =
|
||||
JS_NewCompartmentAndGlobalObject(
|
||||
self.ptr,
|
||||
addr_of(globcls.jsclass),
|
||||
&*globcls as *JSClass,
|
||||
null());
|
||||
result(JS_InitStandardClasses(self.ptr, globobj)).chain { |_ok|
|
||||
ok(self.rooted_obj(globobj))
|
||||
ok({name_pool: np,
|
||||
global_class: globcls,
|
||||
global_obj: self.rooted_obj(globobj)})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -93,6 +97,15 @@ impl methods for cx {
|
|||
}
|
||||
}
|
||||
|
||||
// ___________________________________________________________________________
|
||||
// compartment
|
||||
|
||||
type compartment = {
|
||||
name_pool: name_pool,
|
||||
global_class: @JSClass,
|
||||
global_obj: jsobj
|
||||
};
|
||||
|
||||
// ___________________________________________________________________________
|
||||
// objects
|
||||
|
||||
|
@ -112,7 +125,7 @@ mod test {
|
|||
let gc = jsglobal::global_class();
|
||||
cx.new_global(gc).chain {
|
||||
|glob|
|
||||
str::as_bytes("x = 1;") {
|
||||
str::as_bytes("print(\"1\");") {
|
||||
|bytes|
|
||||
cx.evaluate_script(glob, bytes, "test", 1u)
|
||||
};
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
import jsapi::*;
|
||||
import jsapi::bindgen::*;
|
||||
import ptr::null;
|
||||
import jsutil::*;
|
||||
import name_pool::{name_pool, methods};
|
||||
|
||||
crust fn PropertyStub(++arg0: *JSContext,
|
||||
++arg1: *JSObject,
|
||||
|
@ -36,34 +38,55 @@ crust fn ConvertStub(++arg0: *JSContext,
|
|||
JS_ConvertStub(arg0, arg1, arg2, arg3)
|
||||
}
|
||||
|
||||
fn global_class() -> js::named_class {
|
||||
let name = "global";
|
||||
let c_str = str::as_c_str(name) { |bytes| bytes };
|
||||
@{name: name, // in theory, this should *move* the str in here..
|
||||
jsclass: {name: c_str, // ...and so this ptr ought to be valid.
|
||||
flags: 0x48000_u32,
|
||||
addProperty: PropertyStub,
|
||||
delProperty: PropertyStub,
|
||||
getProperty: PropertyStub,
|
||||
setProperty: StrictPropertyStub,
|
||||
enumerate: EnumerateStub,
|
||||
resolve: ResolveStub,
|
||||
convert: ConvertStub,
|
||||
finalize: null(),
|
||||
reserved0: null(),
|
||||
checkAccess: null(),
|
||||
call: null(),
|
||||
construct: null(),
|
||||
xdrObject: null(),
|
||||
hasInstance: null(),
|
||||
trace: null(),
|
||||
reserved1: null(),
|
||||
reserved: (null(), null(), null(), null(), null(), // 05
|
||||
null(), null(), null(), null(), null(), // 10
|
||||
null(), null(), null(), null(), null(), // 15
|
||||
null(), null(), null(), null(), null(), // 20
|
||||
null(), null(), null(), null(), null(), // 25
|
||||
null(), null(), null(), null(), null(), // 30
|
||||
null(), null(), null(), null(), null(), // 35
|
||||
null(), null(), null(), null(), null())}} // 40
|
||||
fn global_class(np: name_pool) -> JSClass {
|
||||
{name: np.add("global"),
|
||||
flags: 0x48000_u32,
|
||||
addProperty: PropertyStub,
|
||||
delProperty: PropertyStub,
|
||||
getProperty: PropertyStub,
|
||||
setProperty: StrictPropertyStub,
|
||||
enumerate: EnumerateStub,
|
||||
resolve: ResolveStub,
|
||||
convert: ConvertStub,
|
||||
finalize: null(),
|
||||
reserved0: null(),
|
||||
checkAccess: null(),
|
||||
call: null(),
|
||||
construct: null(),
|
||||
xdrObject: null(),
|
||||
hasInstance: null(),
|
||||
trace: null(),
|
||||
reserved1: null(),
|
||||
reserved: (null(), null(), null(), null(), null(), // 05
|
||||
null(), null(), null(), null(), null(), // 10
|
||||
null(), null(), null(), null(), null(), // 15
|
||||
null(), null(), null(), null(), null(), // 20
|
||||
null(), null(), null(), null(), null(), // 25
|
||||
null(), null(), null(), null(), null(), // 30
|
||||
null(), null(), null(), null(), null(), // 35
|
||||
null(), null(), null(), null(), null())} // 40
|
||||
}
|
||||
|
||||
crust fn print(cx: *JSContext, argc: uintN, vp: *jsval) {
|
||||
import io::writer_util;
|
||||
|
||||
unsafe {
|
||||
let argv = JS_ARGV(cx, vp);
|
||||
uint::range(0u, argc as uint) { |i|
|
||||
let jsstr = JS_ValueToString(cx, argv[i]);
|
||||
let bytes = JS_EncodeString(cx, jsstr);
|
||||
let str = str::unsafe::from_c_str(bytes);
|
||||
JS_free(cx, unsafe::reinterpret_cast(bytes));
|
||||
io::stdout().write_str(str);
|
||||
io::stdout().write_str("\n");
|
||||
}
|
||||
JS_SET_RVAL(cx, vp, JSVAL_NULL);
|
||||
}
|
||||
}
|
||||
|
||||
fn global_fns(np: name_pool) -> [JSFunctionSpec] {
|
||||
[{name: np.add("print"),
|
||||
call: print,
|
||||
nargs: 0_u16,
|
||||
flags: 0_u16}]
|
||||
}
|
|
@ -68,6 +68,8 @@ mod util {
|
|||
mod content {
|
||||
mod js;
|
||||
mod jsglobal;
|
||||
mod jsutil;
|
||||
mod name_pool;
|
||||
}
|
||||
|
||||
mod opts;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue