Skip to content

Commit

Permalink
integrate spidermonkey (it doesn't do anything yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed May 17, 2012
1 parent 0571f51 commit 6d27ee5
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/etc/jsdefine
@@ -0,0 +1,21 @@
#!/bin/bash
#
# Handy tool for extracting #defines from jsapi.h. For example:
# ./jsdefine %x JSCLASS_GLOBAL_FLAGS
# outputs
# 48000

ETCDIR=$(dirname $0)
INCDIR=${ETCDIR}/../mozjs/dist/include

echo > jsdefine.c
echo '#include "jsapi.h"' >> jsdefine.c
echo '' >> jsdefine.c
echo 'int main() {' >> jsdefine.c
echo ' printf(' >> jsdefine.c
echo ' "'"$1"'\n"', >> jsdefine.c
echo " $2);" >> jsdefine.c
echo '}' >> jsdefine.c

gcc -I ${INCDIR} jsdefine.c -o jsdefine.exe
./jsdefine.exe
2 changes: 1 addition & 1 deletion src/mozjs
Submodule mozjs updated from e8ffa4 to a971b9
11 changes: 10 additions & 1 deletion src/servo/content.rs
Expand Up @@ -4,9 +4,11 @@ export content;
import dom::rcu::writer_methods;
import dom=dom::base;
import layout::layout;
import js::methods;

enum msg {
parse(str),
execute(str),
exit
}

Expand All @@ -29,10 +31,11 @@ fn join_layout(scope: dom::node_scope,
fn content(to_layout: chan<layout::msg>) -> chan<msg> {
task::spawn_listener::<msg> {|from_master|
let scope = dom::node_scope();
let rt = js::rt();
loop {
alt from_master.recv() {
parse(filename) {
#debug["content: Received filename `%s`", filename];
#debug["content: Received filename `%s` to parse", filename];

// Note: we can parse the next document in parallel
// with any previous documents.
Expand All @@ -50,6 +53,12 @@ fn content(to_layout: chan<layout::msg>) -> chan<msg> {
// changes will be isolated.
scope.reader_forked();
}
execute(filename) {
#debug["content: Received filename `%s` to execute", filename];

let cx = rt.cx();
let _glob = cx.new_global(jsglobal::global_class());
}
exit {
to_layout.send(layout::exit);
break;
Expand Down
79 changes: 79 additions & 0 deletions src/servo/content/js.rs
@@ -0,0 +1,79 @@
import jsapi::*;
import jsapi::bindgen::*;
import ptr::{null, addr_of};
import result::{result, ok, err, extensions};

export rt;
export methods;
export cx;
export named_class;
export jsobj;

const default_heapsize: u32 = 8_u32 * 1024_u32 * 1024_u32;
const default_stacksize: uint = 8192u;
const ERR: JSBool = 0_i32;

fn result(n: JSBool) -> result<(),()> {
if n != ERR {ok(())} else {err(())}
}

type named_class = @{
name: str,
jsclass: JSClass
};

// ___________________________________________________________________________
// runtimes

type rt = @rt_rsrc;

resource rt_rsrc(self: {ptr: *JSRuntime}) {
JS_Finish(self.ptr)
}

fn rt() -> rt {
@rt_rsrc({ptr: JS_Init(default_heapsize)})
}

impl methods for rt {
fn cx() -> cx {
@cx_rsrc({ptr: JS_NewContext(self.ptr, default_stacksize)})
}
}

// ___________________________________________________________________________
// contexts

type cx = @cx_rsrc;
resource cx_rsrc(self: {ptr: *JSContext}) {
JS_DestroyContext(self.ptr);
}

impl methods for cx {
fn rooted_obj(obj: *JSObject) -> jsobj {
let jsobj = @jsobj_rsrc({cx: self.ptr, obj: obj});
JS_AddObjectRoot(self.ptr, ptr::addr_of(jsobj.obj));
jsobj
}

fn new_global(globcls: named_class) -> result<jsobj,()> {
let globobj =
JS_NewCompartmentAndGlobalObject(
self.ptr,
addr_of(globcls.jsclass),
null());
result(JS_InitStandardClasses(self.ptr, globobj)).chain { |_ok|
ok(self.rooted_obj(globobj))
}
}
}

// ___________________________________________________________________________
// objects

type jsobj = @jsobj_rsrc;

resource jsobj_rsrc(self: {cx: *JSContext, obj: *JSObject}) {
JS_RemoveObjectRoot(self.cx, ptr::addr_of(self.obj));
}

69 changes: 69 additions & 0 deletions src/servo/content/jsglobal.rs
@@ -0,0 +1,69 @@
// Definition for the global object that we use:

import jsapi::*;
import jsapi::bindgen::*;
import ptr::null;

crust fn PropertyStub(++arg0: *JSContext,
++arg1: *JSObject,
++arg2: jsid,
++arg3: *jsval) -> JSBool {
JS_PropertyStub(arg0, arg1, arg2, arg3)
}

crust fn StrictPropertyStub(++arg0: *JSContext,
++arg1: *JSObject,
++arg2: jsid,
++arg3: JSBool,
++arg4: *jsval) -> JSBool {
JS_StrictPropertyStub(arg0, arg1, arg2, arg3, arg4)
}

crust fn EnumerateStub(++arg0: *JSContext, ++arg1: *JSObject) -> JSBool {
JS_EnumerateStub(arg0, arg1)
}

crust fn ResolveStub(++arg0: *JSContext,
++arg1: *JSObject,
++arg2: jsid) -> JSBool {
JS_ResolveStub(arg0, arg1, arg2)
}

crust fn ConvertStub(++arg0: *JSContext,
++arg1: *JSObject,
++arg2: JSType,
++arg3: *jsval) -> JSBool {
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
}
8 changes: 7 additions & 1 deletion src/servo/engine.rs
Expand Up @@ -19,7 +19,13 @@ fn engine<S: renderer::sink send>(sink: S) -> comm::chan<msg> {

loop {
alt self_ch.recv() {
load_url(url) { content.send(content::parse(url)) }
load_url(url) {
if url.ends_with(".js") {
content.send(content::execute(url))
} else {
content.send(content::parse(url))
}
}
exit(sender) {
content.send(content::exit);
layout.send(layout::layout::exit);
Expand Down
8 changes: 7 additions & 1 deletion src/servo/servo.rc
Expand Up @@ -12,6 +12,8 @@ use azure;
use js;
use harfbuzz;

import js::jsapi;

mod dom {
mod base;
mod rcu;
Expand Down Expand Up @@ -57,6 +59,10 @@ mod util {
mod tree;
}

mod content;
mod content {
mod js;
mod jsglobal;
}

mod opts;
mod engine;

0 comments on commit 6d27ee5

Please sign in to comment.