Skip to content

Commit dfc450d

Browse files
committed
Update for latest Rust
1 parent 3ad7e09 commit dfc450d

22 files changed

+357
-390
lines changed

Diff for: Cargo.toml

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22

3-
name = "js.rs"
3+
name = "js"
44
version = "0.0.1"
55
readme = "README.md"
66
authors = ["Tom Bebbington <[email protected]>"]
@@ -14,9 +14,11 @@ path = "src/lib/lib.rs"
1414

1515
[[bin]]
1616

17-
name = "js.rs"
17+
name = "js"
1818
path = "src/bin/bin.rs"
1919

2020
[dependencies.jit]
21+
git = "https://github.com/TomBebbington/jit.rs.git"
2122

22-
git = "https://github.com/TomBebbington/jit.rs.git"
23+
[dependencies.url]
24+
git = "https://github.com/servo/rust-url"

Diff for: src/bin/bin.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![crate_id = "github.com/TomBebbington/js.rs"]
21
#![comment = "Javascript parsing and execution command line tool"]
32
#![license = "MIT"]
43
#![crate_type = "bin"]
@@ -41,14 +40,14 @@ pub fn main() {
4140
None if m.opt_present("h") => {
4241
println!("{}", getopts::usage("Usage: js.rs [OPTIONS] [INPUT]", opts));
4342
},
44-
None if m.opt_present("t") || (m.free.len() >= 2 && m.free.get(1).as_slice() == "test") => {
43+
None if m.opt_present("t") || (m.free.len() >= 2 && m.free[1].as_slice() == "test") => {
4544
Tests::new().run();
4645
},
47-
None if m.opt_present("i") || (m.free.len() >= 2 && m.free.get(1).as_slice() == "interactive") => {
46+
None if m.opt_present("i") || (m.free.len() >= 2 && m.free[1].as_slice() == "interactive") => {
4847
Interactive::new().run();
4948
},
5049
None if m.free.len() >= 2 => {
51-
Runner::new(m.free.get(1).clone()).run();
50+
Runner::new(m.free[1].clone()).run();
5251
},
5352
None => {
5453
println!("{}", getopts::short_usage("Usage: js.rs [OPTIONS] [INPUT]", opts));

Diff for: src/bin/interactive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct Interactive<'a> {
1818
}
1919
impl<'a> Interactive<'a> {
2020
/// Create a new interactive mode info
21-
pub fn new() -> Interactive {
21+
pub fn new() -> Interactive<'a> {
2222
Interactive {
2323
context: Context::new(),
2424
executor: Executor::new(&Default::default()),

Diff for: src/bin/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub struct Tests<'a> {
3636
}
3737
impl<'a> Tests<'a> {
3838
/// Create a new unit tester
39-
pub fn new() -> Tests {
39+
pub fn new() -> Tests<'a> {
4040
Tests {
4141
context: Context::new()
4242
}
@@ -47,8 +47,8 @@ impl<'a> Tests<'a> {
4747
if args.len() < 2 {
4848
return Err(to_value("'assert' function expects assertion and description arguments"));
4949
}
50-
let val : bool = from_value(*args.get(0)).unwrap();
51-
let desc : Value = *args.get(1);
50+
let val : bool = from_value(args[0]).unwrap();
51+
let desc : Value = args[1];
5252
if val {
5353
Ok(desc)
5454
} else {

Diff for: src/lib/back/compiler.rs

+19-17
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct JitCompiler<'a> {
2424
impl<'a> JitCompiler<'a> {
2525
/// Construct a new JIT Compiler on the given context
2626
pub fn new(context: &'a Context) -> JitCompiler<'a> {
27-
let main_t = get_type::<fn(*const int, *const int, *const int) -> *const int>();
27+
let main_t = get_type::<fn(*mut int, *mut int, *mut int) -> *mut int>();
2828
JitCompiler {
2929
curr: Function::new(context, main_t)
3030
}
@@ -33,21 +33,23 @@ impl<'a> JitCompiler<'a> {
3333
let bool_t = get_type::<bool>();
3434
let val_kind = val.get_type().get_kind();
3535
let convert = |v| self.curr.insn_convert(&v, bool_t.clone(), false);
36-
if val_kind.contains(SysBool) {
37-
val
38-
} else if val_kind.contains(Float64) {
39-
let zero = 0.0f64.compile(&self.curr);
40-
let not_zero = self.curr.insn_neq(&val, &zero);
41-
let not_nan = !self.curr.insn_is_nan(&val);
42-
convert(not_zero & not_nan)
43-
} else if val_kind.contains(Int) || val_kind.contains(UInt) || val_kind.contains(NInt) || val_kind.contains(NUInt) {
44-
let zero = 0i.compile(&self.curr);
45-
convert(self.curr.insn_neq(&val, &zero))
46-
} else if val_kind.contains(Pointer) {
47-
let one = 1i.compile(&self.curr);
48-
convert(self.curr.insn_gt(&val, &one))
49-
} else {
50-
convert(val)
36+
match val_kind {
37+
SysBool => val,
38+
Float64 => {
39+
let zero = 0.0f64.compile(&self.curr);
40+
let not_zero = self.curr.insn_neq(&val, &zero);
41+
let not_nan = !self.curr.insn_is_nan(&val);
42+
convert(not_zero & not_nan)
43+
},
44+
Int | UInt | NInt | NUInt => {
45+
let zero = 0i.compile(&self.curr);
46+
convert(self.curr.insn_neq(&val, &zero))
47+
},
48+
Pointer => {
49+
let one = 1i.compile(&self.curr);
50+
convert(self.curr.insn_gt(&val, &one))
51+
},
52+
_ => convert(val)
5153
}
5254
}
5355
fn undefined(&'a self) -> Value<'a> {
@@ -159,7 +161,7 @@ impl<'a> Compiler<'a, (Value<'a>, &'a Function<'a>)> for JitCompiler<'a> {
159161
}
160162
fn compile_return(&'a self, val:Option<Box<Expr>>) -> CompiledValue<'a> {
161163
match val {
162-
Some(val) => {
164+
Some(box ref val) => {
163165
let (c_val, _) = self.compile(val);
164166
self.curr.insn_return(&c_val)
165167
},

Diff for: src/lib/back/executor.rs

+46-41
Original file line numberDiff line numberDiff line change
@@ -49,49 +49,54 @@ impl<'a> Executor<(JITVal<'a>, &'a Function<'a>)> for JitExecutor {
4949
fn convert_to_value<'a>(func:&Function<'a>, val:&'a JITVal<'a>) -> JITVal<'a> {
5050
let val_type = val.get_type();
5151
let val_kind = val_type.get_kind();
52-
if val_kind.contains(SysBool) || val_kind.contains(UByte) {
53-
let bool_value = to_value::<bool>;
54-
let sig = get_type::<fn(bool) -> *const int>();
55-
func.insn_call_native1(Some("bool_value"), bool_value, sig, &mut [val])
56-
} else if val_kind.contains(Pointer) {
57-
let ref_t = val_type.get_ref();
58-
if ref_t.get_kind().contains(SysChar) {
59-
fn string_value(val: &i8) -> JSVal {
60-
unsafe {
61-
let text = CString::new(val, false);
62-
to_value(text.as_str().unwrap().into_string())
52+
match val_kind {
53+
SysBool | UByte => {
54+
let bool_value = to_value::<bool>;
55+
let sig = get_type::<fn(bool) -> &'static int>();
56+
func.insn_call_native1(Some("bool_value"), bool_value, sig, [val])
57+
},
58+
Pointer => {
59+
let ref_t = val_type.get_ref();
60+
if ref_t.get_kind() == SysChar {
61+
fn string_value(val: &i8) -> JSVal {
62+
unsafe {
63+
let text = CString::new(val, false);
64+
to_value(text.as_str().unwrap().into_string())
65+
}
6366
}
64-
}
65-
let sig = get_type::<fn(String) -> *const int>();
66-
func.insn_call_native1(Some("string_value"), string_value, sig, &mut [val])
67-
} else {
68-
fn ptr_value(ptr: &i8) -> JSVal {
69-
match ptr.to_uint() {
70-
Some(0u) => JSVal::undefined(),
71-
Some(1u) => JSVal {
72-
ptr: box(GC) VNull
73-
},
74-
ptr => fail!("Invalid pointer: {}", ptr)
67+
let sig = get_type::<fn(String) -> &'static int>();
68+
func.insn_call_native1(Some("string_value"), string_value, sig, [val])
69+
} else {
70+
fn ptr_value(ptr: &i8) -> JSVal {
71+
match ptr.to_uint() {
72+
Some(0u) => JSVal::undefined(),
73+
Some(1u) => JSVal {
74+
ptr: box(GC) VNull
75+
},
76+
ptr => fail!("Invalid pointer: {}", ptr)
77+
}
7578
}
79+
let sig = get_type::<fn(&'static i8) -> &'static int>();
80+
func.insn_call_native1(Some("ptr_value"), ptr_value, sig, [val])
7681
}
77-
let sig = get_type::<fn(*const i8) -> *const int>();
78-
func.insn_call_native1(Some("ptr_value"), ptr_value, sig, &mut [val])
79-
}
80-
} else if val_kind.contains(Int) || val_kind.contains(UInt) {
81-
let int_value = to_value::<i32>;
82-
let sig = get_type::<fn(i32) -> *const int>();
83-
func.insn_call_native1(Some("int_value"), int_value, sig, &mut [val])
84-
} else if val_kind.contains(NInt) || val_kind.contains(NUInt) {
85-
fn sys_int_value(num:int) -> JSVal {
86-
to_value::<i32>(num as i32)
87-
}
88-
let sig = get_type::<fn(int) -> *const int>();
89-
func.insn_call_native1(Some("sys_int_value"), sys_int_value, sig, &mut [val])
90-
} else if val_kind.contains(Float64) {
91-
let float_value = to_value::<f64>;
92-
let sig = get_type::<fn(f64) -> *const int>();
93-
func.insn_call_native1(Some("float_value"), float_value, sig, &mut [val])
94-
} else {
95-
fail!("Invalid kind {}", val_kind.bits())
82+
},
83+
Int | UInt => {
84+
let int_value = to_value::<i32>;
85+
let sig = get_type::<fn(i32) -> &'static int>();
86+
func.insn_call_native1(Some("int_value"), int_value, sig, [val])
87+
},
88+
NInt | NUInt => {
89+
fn sys_int_value(num:int) -> JSVal {
90+
to_value::<i32>(num as i32)
91+
}
92+
let sig = get_type::<fn(int) -> &'static int>();
93+
func.insn_call_native1(Some("sys_int_value"), sys_int_value, sig, [val])
94+
},
95+
Float64 => {
96+
let float_value = to_value::<f64>;
97+
let sig = get_type::<fn(f64) -> &'static int>();
98+
func.insn_call_native1(Some("float_value"), float_value, sig, [val])
99+
},
100+
_ => fail!("Unexpected type {}", val_kind)
96101
}
97102
}

Diff for: src/lib/front/run/compiler.rs

+30-30
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,48 @@ pub trait Compiler<'a, Compiled> {
1111
fn compile(&'a self, expr:&Expr) -> Compiled {
1212
debug!("Compiling {}", expr);
1313
match expr.def.clone() {
14-
UnaryOpExpr(op, box ref ex) =>
15-
self.compile_unary_op(op, ex),
16-
BinOpExpr(op, box ref left, box ref right) =>
17-
self.compile_bin_op(op, left, right),
14+
UnaryOpExpr(op, box ex) =>
15+
self.compile_unary_op(op, &ex),
16+
BinOpExpr(op, box left, box right) =>
17+
self.compile_bin_op(op, &left, &right),
1818
ConstExpr(ref c) =>
1919
self.compile_const(c),
2020
LocalExpr(l) =>
2121
self.compile_local(l),
2222
BlockExpr(vals) =>
2323
self.compile_block(vals),
24-
GetConstFieldExpr(box ref obj, field) =>
25-
self.compile_get_const_field(obj, field),
26-
GetFieldExpr(box ref obj, box ref field) =>
27-
self.compile_get_field(obj, field),
28-
CallExpr(box ref func, args) =>
29-
self.compile_call(func, args),
30-
WhileLoopExpr(box ref cond, box ref expr) =>
31-
self.compile_while_loop(cond, expr),
32-
IfExpr(box ref cond, box ref if_expr, else_expr) =>
33-
self.compile_if(cond, if_expr, else_expr),
34-
SwitchExpr(box ref value, cases, default) =>
35-
self.compile_switch(value, cases, default),
36-
ObjectDeclExpr(box ref fields) =>
37-
self.compile_object_decl(fields),
24+
GetConstFieldExpr(box obj, field) =>
25+
self.compile_get_const_field(&obj, field),
26+
GetFieldExpr(box obj, box field) =>
27+
self.compile_get_field(&obj, &field),
28+
CallExpr(box func, args) =>
29+
self.compile_call(&func, args),
30+
WhileLoopExpr(box cond, box expr) =>
31+
self.compile_while_loop(&cond, &expr),
32+
IfExpr(box cond, box if_expr, else_expr) =>
33+
self.compile_if(&cond, &if_expr, else_expr),
34+
SwitchExpr(box value, cases, default) =>
35+
self.compile_switch(&value, cases, default),
36+
ObjectDeclExpr(box fields) =>
37+
self.compile_object_decl(&fields),
3838
ArrayDeclExpr(values) =>
3939
self.compile_array_decl(values),
40-
FunctionDeclExpr(name, args, box ref ret) =>
41-
self.compile_function_decl(name, args, ret),
42-
ArrowFunctionDeclExpr(args, box ref ret) =>
43-
self.compile_arrow_function_decl(args, ret),
44-
ConstructExpr(box ref func, args) =>
45-
self.compile_construct(func, args),
40+
FunctionDeclExpr(name, args, box ret) =>
41+
self.compile_function_decl(name, args, &ret),
42+
ArrowFunctionDeclExpr(args, box ret) =>
43+
self.compile_arrow_function_decl(args, &ret),
44+
ConstructExpr(box func, args) =>
45+
self.compile_construct(&func, args),
4646
ReturnExpr(val) =>
4747
self.compile_return(val),
48-
ThrowExpr(box ref val) =>
49-
self.compile_throw(val),
50-
AssignExpr(box ref left, box ref right) =>
51-
self.compile_assign(left, right),
48+
ThrowExpr(box val) =>
49+
self.compile_throw(&val),
50+
AssignExpr(box left, box right) =>
51+
self.compile_assign(&left, &right),
5252
VarDeclExpr(vars) =>
5353
self.compile_var_decl(vars),
54-
TypeOfExpr(box ref expr) =>
55-
self.compile_typeof(expr)
54+
TypeOfExpr(box expr) =>
55+
self.compile_typeof(&expr)
5656
}
5757
}
5858
/// Compile a unary operation

Diff for: src/lib/front/stdlib/console.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn error(args:Vec<Value>, _:Value, _:Value, _:Value) -> ResultValue {
1313
let args : Vec<String> = args.iter().map(|x|from_value::<String>(*x).unwrap()).collect();
1414
match writeln!(&mut stderr().unwrap(), "{}: {}", strftime("%X", &now()), args.connect(" ")) {
1515
Ok(_) => Ok(Value::undefined()),
16-
Err(io_error) => Err(to_value(io_error.to_str()))
16+
Err(io_error) => Err(to_value(io_error.to_string()))
1717
}
1818
}
1919
/// Create a new `console` object

Diff for: src/lib/front/stdlib/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use front::stdlib::function::Function;
55
/// Create a new error
66
pub fn make_error(args:Vec<Value>, _:Value, _:Value, this:Value) -> ResultValue {
77
if args.len() >= 1 {
8-
this.set_field("message", to_value(args.get(0).to_str().into_string()));
8+
this.set_field("message", args[0]);
99
}
1010
Ok(Value::undefined())
1111
}

Diff for: src/lib/front/stdlib/json.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ use front::stdlib::function::Function;
33
use serialize::json::{ToJson, from_str};
44
/// Parse a JSON string into a Javascript object
55
pub fn parse(args:Vec<Value>, _:Value, _:Value, _:Value) -> ResultValue {
6-
match from_str(args.get(0).to_str().as_slice()) {
6+
let arg = args[0];
7+
match from_str(arg.to_string().as_slice()) {
78
Ok(json) => {
89
Ok(to_value(json))
910
},
1011
Err(err) => {
11-
Err(to_value(err.to_str()))
12+
Err(to_value(err.to_string()))
1213
}
1314
}
1415
}
1516
/// Process a Javascript object into a JSON string
1617
pub fn stringify(args:Vec<Value>, _:Value, _:Value, _:Value) -> ResultValue {
17-
let obj = args.get(0);
18+
let obj = args[0];
1819
let json = obj.to_json();
1920
Ok(to_value(json.to_pretty_str()))
2021
}

0 commit comments

Comments
 (0)