Skip to content

Commit

Permalink
Pull latest dom.js; expose ability to synchronously read a file from …
Browse files Browse the repository at this point in the history
…disk to js; implement file urls for xmlhttprequest (hackily); load a test html file in test.js.
  • Loading branch information
fzzzy committed Mar 2, 2012
1 parent 438fa0f commit 2117ff3
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 17 deletions.
11 changes: 7 additions & 4 deletions dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -14181,8 +14181,6 @@ defineLazyProperty(impl, "HTMLScriptElement", function() {

var script = this;
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.send();

// Web workers support this handler but not the old
// onreadystatechange handler
Expand Down Expand Up @@ -14215,6 +14213,9 @@ defineLazyProperty(impl, "HTMLScriptElement", function() {
script.ownerDocument._parser.resume();
}
}

xhr.open("GET", url);
xhr.send();
}
}
else {
Expand Down Expand Up @@ -27668,8 +27669,7 @@ Location.prototype = Object.create(URLDecompositionAttributes.prototype, {
// This is just something hacked together.
// The real algorithm is: http://www.whatwg.org/specs/web-apps/current-work/multipage/history.html#navigate
var xhr = new XMLHttpRequest();
xhr.open("GET", newurl);
xhr.send();

xhr.onload = function() {
var olddoc = self._window.document;
var parser = new HTMLParser(newurl);
Expand All @@ -27690,6 +27690,9 @@ Location.prototype = Object.create(URLDecompositionAttributes.prototype, {
parser.parse(xhr.responseText, true);
};

xhr.open("GET", newurl);
xhr.send();

}),

replace: constant(function(url) {
Expand Down
7 changes: 7 additions & 0 deletions foo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<head><title>Hello</title></head>
<body>
<h1>Hello World</h1>
</body>
</html>
77 changes: 77 additions & 0 deletions spidermonkeyrustext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cstring>
#include <stdint.h>
#include <pthread.h>
#include <errno.h>

/*
* Rust API declarations.
Expand Down Expand Up @@ -240,8 +241,84 @@ static JSBool JSRust_Print(JSContext *cx, uintN argc, jsval *vp) {
return JS_TRUE;
}

static JSString *
FileAsString(JSContext *cx, const char *pathname)
{
FILE *file;
JSString *str = NULL;
size_t len, cc;
char *buf;

file = fopen(pathname, "rb");
if (!file) {
JS_ReportError(cx, "can't open %s: %s", pathname, strerror(errno));
return NULL;
}

if (fseek(file, 0, SEEK_END) != 0) {
JS_ReportError(cx, "can't seek end of %s", pathname);
} else {
len = ftell(file);
if (fseek(file, 0, SEEK_SET) != 0) {
JS_ReportError(cx, "can't seek start of %s", pathname);
} else {
buf = (char*) JS_malloc(cx, len + 1);
if (buf) {
cc = fread(buf, 1, len, file);
if (cc != len) {
JS_ReportError(cx, "can't read %s: %s", pathname,
(ptrdiff_t(cc) < 0) ? strerror(errno) : "short read");
} else {
jschar *ucbuf;
size_t uclen;

len = (size_t)cc;

if (!JS_DecodeUTF8(cx, buf, len, NULL, &uclen)) {
JS_ReportError(cx, "Invalid UTF-8 in file '%s'", pathname);
return NULL;
}

ucbuf = (jschar*)malloc(uclen * sizeof(jschar));
JS_DecodeUTF8(cx, buf, len, ucbuf, &uclen);
str = JS_NewUCStringCopyN(cx, ucbuf, uclen);
free(ucbuf);
}
JS_free(cx, buf);
}
}
}
fclose(file);

return str;
}

static JSBool
JSRust_Read(JSContext *cx, uintN argc, jsval *vp)
{
JSString *str;

if (!argc)
return JS_FALSE;

str = JS_ValueToString(cx, JS_ARGV(cx, vp)[0]);
if (!str)
return JS_FALSE;
JSAutoByteString filename(cx, str);
if (!filename)
return JS_FALSE;

const char *pathname = filename.ptr();

if (!(str = FileAsString(cx, pathname)))
return JS_FALSE;
*vp = STRING_TO_JSVAL(str);
return JS_TRUE;
}

static JSFunctionSpec global_functions[] = {
JS_FN("print", JSRust_Print, 0, 0),
JS_FN("jsrust_read", JSRust_Read, 0, 0),
JS_FS_END
};

Expand Down
8 changes: 6 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@

print("Hello, world!");
/*print(document);
print(document);
document._setMutationHandler(function(mut) {
print(JSON.stringify(mut));
});*/
});

// Hack. File urls are not correctly parsed right now,
// but this syntax just happens to work.
window.location = "file:foo.html";

postMessage(4, [12,34,"Hello!"]);

Expand Down
11 changes: 8 additions & 3 deletions test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ enum out_msg {
stderr(str),
spawn(str, str),
cast(str, str),
exitproc
exitproc,
}


Expand Down Expand Up @@ -105,6 +105,7 @@ fn on_js_msg(myid : int, out : chan<out_msg>, m : js::jsrust_message, childid :
fn on_ctl_msg(myid : int, cx : js::context, global : js::object, msg : ctl_msg, checkwait : js::script, loadurl : js::script) {
alt msg {
load_url(x) {
log(core::error, x);
js::set_data_property(cx, global, x);
js::execute_script(cx, global, loadurl);
}
Expand Down Expand Up @@ -153,12 +154,16 @@ fn run_actor(myid : int, myurl : str, maxbytes : u32, out : chan<out_msg>, sendc

run_script(cx, global, "xmlhttprequest.js");
run_script(cx, global, "dom.js");
run_script(cx, global, "layout.js");

let checkwait = js::compile_script(
cx, global, str::bytes("if (XMLHttpRequest.requests_outstanding === 0) jsrust_exit();"), "io", 0u),
loadurl = js::compile_script(cx, global, str::bytes("try { _resume(5, _data, 0) } catch (e) { print(e + '\\n' + e.stack) } _data = undefined;"), "io", 0u);
loadurl = js::compile_script(cx, global, str::bytes("try { _resume(9, _data, 0) } catch (e) { print(e + '\\n' + e.stack) } _data = undefined;"), "io", 0u);

if str::len(myurl) > 4u && str::eq(str::slice(myurl, 0u, 4u), "http") {
if str::len(myurl) > 4u && (
str::eq(str::slice(myurl, 0u, 4u), "http") ||
str::eq(str::slice(myurl, 0u, 4u), "file")) {
log(core::error, "loadurl");
send(msg_chan, load_url(myurl));
} else {
send(msg_chan, load_script(myurl));
Expand Down
25 changes: 17 additions & 8 deletions xmlhttprequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ XMLHttpRequest.prototype = {
port = 80;
} else if (parts.scheme === 'https') {
port = 443;
} else if (parts.scheme === 'file') {
this.status = 0;
this.statusText = "";
// Hack: Work around dom.js giving us file:/foo/bar urls
this.responseText = jsrust_read(url.slice(5));
this.readyState = XMLHttpRequest.prototype.DONE;
this.onreadystatechange();
return;
} else {
throw new Error("Unsupported scheme: " + parts.scheme);
}
Expand Down Expand Up @@ -114,13 +122,13 @@ XMLHttpRequest.prototype = {
if (data) {
this._request += data;
}
if (this.connected) {
jsrust_send(this._fd, this._request);
jsrust_recv(this._fd, 32768);
this._request = "";
} else {
//jsrust_send(this._fd, "");
}
if (this.connected) {
jsrust_send(this._fd, this._request);
jsrust_recv(this._fd, 32768);
this._request = "";
} else {
//jsrust_send(this._fd, "");
}
},
abort: function abort() {
// TODO ???
Expand All @@ -139,14 +147,15 @@ XMLHttpRequest.prototype = {

global._resume = function _resume(what, data, req_id) {
//print("Handling request. Total:", XMLHttpRequest.requests_outstanding);
//print("_resume", what, data, req_id);
var xhr = _xhrs[req_id] || null;
if (what === CONN) {
xhr.readyState = XMLHttpRequest.prototype.OPENED;
xhr.onreadystatechange.apply(xhr);
if (xhr._request.length) {
jsrust_send(xhr._fd, xhr._request);
}
this.connected = true;
this.connected = true;
} else if (what === SEND) {
jsrust_recv(xhr._fd, 32768);
// TODO need to get number of bytes actually written back into js
Expand Down

0 comments on commit 2117ff3

Please sign in to comment.