1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
//! A high-level wrapper around the [V8 Javascript engine][1].
//!
//! # Usage
//!
//! First, you need to create an [`Isolate`](isolate/struct.Isolate.html).  An isolate is a VM
//! instance with its own heap.  You should most likely create one per thread and re-use it as much
//! as possible.
//!
//! Then, you need to create a [`Context`](context/struct.Context.html).  A context is an execution
//! environment that allows separate, unrelated, JavaScript code to run in a single instance of V8.
//! You must explicitly specify the context in which you want any JavaScript code to be run.  You
//! should keep track of the context of a script manually as part of your application.
//!
//! # Example
//!
//! ```
//! use v8::{self, value};
//!
//! // Create a V8 heap
//! let isolate = v8::Isolate::new();
//! // Create a new context of execution
//! let context = v8::Context::new(&isolate);
//!
//! // Load the source code that we want to evaluate
//! let source = value::String::from_str(&isolate, "'Hello, ' + 'World!'");
//!
//! // Compile the source code.  `unwrap()` panics if the code is invalid,
//! // e.g. if there is a syntax  error.
//! let script = v8::Script::compile(&isolate, &context, &source).unwrap();
//!
//! // Run the compiled script.  `unwrap()` panics if the code threw an
//! // exception.
//! let result = script.run(&context).unwrap();
//!
//! // Convert the result to a value::String.
//! let result_str = result.to_string(&context);
//!
//! // Success!
//! assert_eq!("Hello, World!", result_str.value());
//! ```
//!
//! # Isolate foreground tasks
//!
//! Javascript can produce "deferred" or "time-outed" tasks that need to run on the main thread.
//! Additionally, V8 has a bunch of internal tasks it wants to perform regularly (for example GC).
//! The user should therefore call `isolate.run_enqueued_tasks()` regularly to allow these tasks to
//! run.
//!
//! [1]: https://developers.google.com/v8/

#![cfg_attr(all(feature="unstable", test), feature(test))]

#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate lazy_static;
extern crate num_cpus;
extern crate v8_sys;

mod allocator;
mod platform;
#[macro_use]
mod util;

pub mod context;
pub mod error;
pub mod isolate;
pub mod script;
pub mod template;
pub mod value;

pub use context::Context;
pub use isolate::Isolate;
pub use script::Script;
pub use value::Value;

#[cfg(test)]
mod tests {
    use super::*;

    fn eval(source: &str) -> error::Result<(Isolate, Context, Value)> {
        let isolate = Isolate::new();
        let context = Context::new(&isolate);
        let name = value::String::from_str(&isolate, "test.js");
        let source = value::String::from_str(&isolate, source);
        let script = try!(Script::compile_with_name(&isolate, &context, &name, &source));
        let result = try!(script.run(&context));
        Ok((isolate, context, result))
    }

    #[test]
    fn hello_world() {
        let (_, _, v) = eval("'Hello, ' + 'World!'").unwrap();
        assert!(v.is_string());
        let result = v.into_string().unwrap().value();
        assert_eq!("Hello, World!", result);
    }

    #[test]
    fn eval_undefined() {
        let (_, _, v) = eval("undefined").unwrap();
        assert!(v.is_undefined());
    }

    #[test]
    fn eval_null() {
        let (_, _, v) = eval("null").unwrap();
        assert!(v.is_null());
    }

    #[test]
    fn eval_bool_false() {
        let (_, c, v) = eval("false").unwrap();
        assert!(v.is_boolean());
        assert!(v.is_false());
        assert_eq!(false, v.boolean_value(&c));
        let v = v.into_boolean().unwrap();
        assert_eq!(false, v.value());
    }

    #[test]
    fn eval_bool_true() {
        let (_, c, v) = eval("true").unwrap();
        assert!(v.is_boolean());
        assert!(v.is_true());
        assert_eq!(true, v.boolean_value(&c));
        let v = v.into_boolean().unwrap();
        assert_eq!(true, v.value());
    }

    #[test]
    fn eval_string() {
        let (_, c, v) = eval("'foo'").unwrap();
        assert!(v.is_string());
        assert_eq!("foo", v.to_string(&c).value());
        let v = v.into_string().unwrap();
        assert_eq!("foo", v.value());
    }

    #[test]
    fn eval_string_length() {
        let (_, _, v) = eval("'foo'").unwrap();
        assert!(v.is_string());
        let v = v.into_string().unwrap();
        assert_eq!(3, v.length());
    }

    #[test]
    fn eval_string_utf8_length_1() {
        let (_, _, v) = eval("'a'").unwrap();
        assert!(v.is_string());
        let v = v.into_string().unwrap();
        assert_eq!(1, v.utf8_length());
    }

    #[test]
    fn eval_string_utf8_length_2() {
        let (_, _, v) = eval("'ä'").unwrap();
        assert!(v.is_string());
        let v = v.into_string().unwrap();
        assert_eq!(2, v.utf8_length());
    }

    #[test]
    fn eval_string_utf8_length_3() {
        let (_, _, v) = eval("'௵'").unwrap();
        assert!(v.is_string());
        let v = v.into_string().unwrap();
        assert_eq!(3, v.utf8_length());
    }

    #[test]
    fn eval_string_utf8_length_4() {
        let (_, _, v) = eval("'𒀰'").unwrap();
        assert!(v.is_string());
        let v = v.into_string().unwrap();
        assert_eq!(4, v.utf8_length());
    }

    #[test]
    fn eval_string_edge_cases() {
        let (_, _, v) = eval(r#"'foo\u0000\uffff௵𒀰\uD808\uDC30'"#).unwrap();
        assert!(v.is_string());
        let v = v.into_string().unwrap();
        assert_eq!("foo\u{0000}\u{ffff}௵𒀰𒀰", v.value());
    }

    #[test]
    fn eval_uint32() {
        let (_, c, v) = eval("42").unwrap();
        assert!(v.is_number());
        assert!(v.is_uint32());
        assert_eq!(42, v.uint32_value(&c));
        let v = v.into_uint32().unwrap();
        assert_eq!(42, v.value());
    }

    #[test]
    fn eval_int32() {
        let (_, c, v) = eval("-42").unwrap();
        assert!(v.is_number());
        assert!(v.is_int32());
        assert_eq!(-42, v.int32_value(&c));
        let v = v.into_int32().unwrap();
        assert_eq!(-42, v.value());
    }

    #[test]
    fn eval_integer() {
        // Use largest possible integer n such that all values of 0..n
        // can be represented in Javascript
        let (_, c, v) = eval("9007199254740992").unwrap();
        assert!(v.is_number());
        assert_eq!(9007199254740992, v.integer_value(&c));
    }

    #[test]
    fn eval_function() {
        let (i, c, v) = eval("(function(a, b) { return a + b; })").unwrap();
        let a = value::Integer::new(&i, 3);
        let b = value::Integer::new(&i, 4);
        let f = v.into_function().unwrap();
        let r = f.call(&c, &[&a, &b]).unwrap();
        assert!(r.is_int32());
        assert_eq!(7, r.int32_value(&c));
    }

    #[test]
    fn eval_equals_true() {
        let (i, c, v) = eval("({a: '', b: []})").unwrap();
        assert!(v.is_object());
        let v = v.into_object().unwrap();
        let a_key = value::String::from_str(&i, "a");
        let b_key = value::String::from_str(&i, "b");
        assert!(v.get(&c, &a_key).equals(&c, &v.get(&c, &b_key)));
    }

    #[test]
    fn eval_equals_false() {
        let (i, c, v) = eval("({a: '', b: 1})").unwrap();
        assert!(v.is_object());
        let v = v.into_object().unwrap();
        let a_key = value::String::from_str(&i, "a");
        let b_key = value::String::from_str(&i, "b");
        assert!(!v.get(&c, &a_key).equals(&c, &v.get(&c, &b_key)));
    }

    #[test]
    fn eval_strict_equals_true() {
        let (i, c, v) = eval("({a: 2, b: 2})").unwrap();
        assert!(v.is_object());
        let v = v.into_object().unwrap();
        let a_key = value::String::from_str(&i, "a");
        let b_key = value::String::from_str(&i, "b");
        assert!(v.get(&c, &a_key).strict_equals(&v.get(&c, &b_key)));
    }

    #[test]
    fn eval_strict_equals_false() {
        let (i, c, v) = eval("({a: '', b: []})").unwrap();
        assert!(v.is_object());
        let v = v.into_object().unwrap();
        let a_key = value::String::from_str(&i, "a");
        let b_key = value::String::from_str(&i, "b");
        assert!(!v.get(&c, &a_key).strict_equals(&v.get(&c, &b_key)));
    }

    #[test]
    fn eval_same_value_true() {
        let (i, c, v) = eval("(function() { var a = {}; return {a: a, b: a}; })()").unwrap();
        assert!(v.is_object());
        let v = v.into_object().unwrap();
        let a_key = value::String::from_str(&i, "a");
        let b_key = value::String::from_str(&i, "b");
        assert!(v.get(&c, &a_key).same_value(&v.get(&c, &b_key)));
    }

    #[test]
    fn eval_same_value_false() {
        let (i, c, v) = eval("({a: {}, b: {}})").unwrap();
        assert!(v.is_object());
        let v = v.into_object().unwrap();
        let a_key = value::String::from_str(&i, "a");
        let b_key = value::String::from_str(&i, "b");
        assert!(!v.get(&c, &a_key).same_value(&v.get(&c, &b_key)));
    }

    #[test]
    fn eval_function_then_call() {
        let (i, c, v) = eval("(function(a) { return a + a; })").unwrap();
        assert!(v.is_function());
        let f = v.into_function().unwrap();
        let s = value::String::from_str(&i, "123");
        let r = f.call(&c, &[&s]).unwrap();
        assert!(r.is_string());
        assert_eq!("123123", r.into_string().unwrap().value());
    }

    #[test]
    fn eval_function_then_call_with_this() {
        let (i, c, v) = eval("(function() { return this.length; })").unwrap();
        assert!(v.is_function());
        let f = v.into_function().unwrap();
        let s = value::String::from_str(&i, "123");
        let r = f.call_with_this(&c, &s, &[]).unwrap();
        assert!(r.is_int32());
        assert_eq!(3, r.int32_value(&c));
    }

    #[test]
    fn eval_function_then_construct() {
        let (i, c, v) = eval("(function ctor(a) { this.a = a; })").unwrap();
        assert!(v.is_function());
        let f = v.into_function().unwrap();
        let a_key = value::String::from_str(&i, "a");
        let s = value::String::from_str(&i, "123");
        let r = f.call_as_constructor(&c, &[&s]).unwrap();
        assert!(r.is_object());
        let r = r.into_object().unwrap();
        let r = r.get(&c, &a_key);
        assert!(r.is_string());
        let r = r.to_string(&c);
        assert_eq!("123", r.value());
    }

    #[test]
    fn eval_array() {
        let (_, c, v) = eval("[1, true, null]").unwrap();
        assert!(v.is_array());
        let v = v.into_object().unwrap();
        assert!(v.get_index(&c, 0).is_number());
        assert!(v.get_index(&c, 1).is_boolean());
        assert!(v.get_index(&c, 2).is_null());
    }

    #[test]
    fn eval_object() {
        let (i, c, v) = eval("({a: 2, b: true})").unwrap();
        assert!(v.is_object());
        let result = v.into_object().unwrap();
        let a_key = value::String::from_str(&i, "a");
        let b_key = value::String::from_str(&i, "b");
        assert_eq!(2, result.get(&c, &a_key).integer_value(&c));
        assert_eq!(true, result.get(&c, &b_key).boolean_value(&c));
    }

    #[test]
    fn eval_date() {
        let (_, _, v) = eval("new Date(0)").unwrap();
        assert!(v.is_date());
    }

    #[test]
    fn eval_arguments_object() {
        let (_, _, v) = eval("(function() { return arguments; })()").unwrap();
        assert!(v.is_arguments_object());
    }

    #[test]
    fn eval_boolean_object() {
        let (_, _, v) = eval("new Boolean(true)").unwrap();
        assert!(v.is_boolean_object());
    }

    #[test]
    fn eval_number_object() {
        let (_, _, v) = eval("new Number(42)").unwrap();
        assert!(v.is_number_object());
    }

    #[test]
    fn eval_string_object() {
        let (_, _, v) = eval("new String('abc')").unwrap();
        assert!(v.is_string_object());
    }

    #[test]
    fn eval_symbol_object() {
        let (_, _, v) = eval("Object(Symbol('abc'))").unwrap();
        assert!(v.is_symbol_object());
    }

    #[test]
    fn eval_native_error() {
        let (_, _, v) = eval("new Error()").unwrap();
        assert!(v.is_native_error());
    }

    #[test]
    fn eval_reg_exp() {
        let (_, _, v) = eval("/./").unwrap();
        assert!(v.is_reg_exp());
    }

    #[test]
    fn eval_generator_function() {
        let (_, _, v) = eval("(function* () {})").unwrap();
        assert!(v.is_generator_function());
    }

    #[test]
    fn eval_generator_object() {
        let (_, _, v) = eval("(function* () {})()").unwrap();
        assert!(v.is_generator_object());
    }

    #[test]
    fn eval_promise() {
        let (_, _, v) = eval("new Promise(function() {})").unwrap();
        assert!(v.is_promise());
    }

    #[test]
    fn eval_map() {
        let (_, _, v) = eval("new Map()").unwrap();
        assert!(v.is_map());
    }

    #[test]
    fn eval_set() {
        let (_, _, v) = eval("new Set()").unwrap();
        assert!(v.is_set());
    }

    #[test]
    fn eval_map_iterator() {
        // TODO: how?
    }

    #[test]
    fn eval_set_iterator() {
        // TODO: how?
    }

    #[test]
    fn eval_syntax_error() {
        let result = eval("(");

        let error = result.unwrap_err();
        match error.kind() {
            &error::ErrorKind::Javascript(ref msg, _) => {
                assert_eq!("Uncaught SyntaxError: Unexpected end of input", msg);
            }
            x => panic!("Unexpected error kind: {:?}", x),
        }
    }

    #[test]
    fn eval_exception() {
        let result = eval("throw 'x';");

        let error = result.unwrap_err();
        match error.kind() {
            &error::ErrorKind::Javascript(ref msg, _) => {
                assert_eq!("Uncaught x", msg);
            }
            x => panic!("Unexpected error kind: {:?}", x),
        }
    }

    #[test]
    fn eval_exception_stack() {
        let result = eval(r#"
(function() {
  function x() {
    y();
  }
  function y() {
    eval("z()");
  }
  function z() {
    new w();
  }
  function w() {
    throw new Error('x');
  }
  x();
})();
"#);

        let error = result.unwrap_err();
        match error.kind() {
            &error::ErrorKind::Javascript(ref msg, ref stack_trace) => {
                assert_eq!("Uncaught Error: x", msg);
                assert_eq!("    at new w (test.js:13:11)\n    at z (test.js:10:5)\n    at eval \
                            <anon>:1:1\n    at y (test.js:7:5)\n    at x (test.js:4:5)\n    at \
                            test.js:15:3\n    at test.js:16:3\n",
                           format!("{}", stack_trace));
            }
            x => panic!("Unexpected error kind: {:?}", x),
        }
    }

    #[test]
    fn run_native_function_call() {
        let isolate = Isolate::new();
        let context = Context::new(&isolate);

        let function = value::Function::new(&isolate,
                                            &context,
                                            1,
                                            Box::new(|mut info| Ok(info.args.remove(0))));
        let param = value::Integer::new(&isolate, 42);

        let result = function.call(&context, &[&param]).unwrap();
        assert_eq!(42, result.uint32_value(&context));
    }

    #[test]
    fn run_defined_function() {
        let i = Isolate::new();
        let c = Context::new(&i);

        let fi = i.clone();
        let fc = c.clone();
        let f = value::Function::new(&i,
                                     &c,
                                     2,
                                     Box::new(move |info| {
            assert_eq!(2, info.length);
            let ref a = info.args[0];
            assert!(a.is_int32());
            let a = a.int32_value(&fc);
            let ref b = info.args[1];
            assert!(b.is_int32());
            let b = b.int32_value(&fc);

            Ok(value::Integer::new(&fi, a + b).into())
        }));

        let k = value::String::from_str(&i, "f");
        c.global().set(&c, &k, &f);

        let name = value::String::from_str(&i, "test.js");
        let source = value::String::from_str(&i, "f(2, 3)");
        let script = Script::compile_with_name(&i, &c, &name, &source).unwrap();
        let result = script.run(&c).unwrap();

        assert!(result.is_int32());
        assert_eq!(5, result.int32_value(&c));
    }

    fn test_function(info: value::FunctionCallbackInfo) -> Result<value::Value, value::Value> {
        let i = info.isolate;
        let c = i.current_context().unwrap();

        assert_eq!(2, info.length);
        let ref a = info.args[0];
        assert!(a.is_int32());
        let a = a.int32_value(&c);
        let ref b = info.args[1];
        assert!(b.is_int32());
        let b = b.int32_value(&c);

        Ok(value::Integer::new(&i, a + b).into())
    }

    #[test]
    fn run_defined_static_function() {
        let i = Isolate::new();
        let c = Context::new(&i);
        let f = value::Function::new(&i, &c, 2, Box::new(test_function));

        let k = value::String::from_str(&i, "f");
        c.global().set(&c, &k, &f);

        let name = value::String::from_str(&i, "test.js");
        let source = value::String::from_str(&i, "f(2, 3)");
        let script = Script::compile_with_name(&i, &c, &name, &source).unwrap();
        let result = script.run(&c).unwrap();

        assert!(result.is_int32());
        assert_eq!(5, result.int32_value(&c));
    }

    #[test]
    fn run_defined_function_template_instance() {
        let i = Isolate::new();
        let c = Context::new(&i);
        let ft = template::FunctionTemplate::new(&i, &c, Box::new(test_function));
        let f = ft.get_function(&c);

        let k = value::String::from_str(&i, "f");
        c.global().set(&c, &k, &f);

        let name = value::String::from_str(&i, "test.js");
        let source = value::String::from_str(&i, "f(2, 3)");
        let script = Script::compile_with_name(&i, &c, &name, &source).unwrap();
        let result = script.run(&c).unwrap();

        assert!(result.is_int32());
        assert_eq!(5, result.int32_value(&c));
    }

    #[test]
    fn create_object_instance() {
        let i = Isolate::new();
        let c = Context::new(&i);
        value::Object::new(&i, &c);
    }

    #[test]
    fn create_array_instance() {
        let i = Isolate::new();
        let c = Context::new(&i);
        value::Array::new(&i, &c, 42);
    }

    #[test]
    fn create_object_template_instance() {
        let i = Isolate::new();
        let c = Context::new(&i);
        let ot = template::ObjectTemplate::new(&i);
        ot.set("test", &value::Integer::new(&i, 5));

        let o = ot.new_instance(&c);
        let k = value::String::from_str(&i, "o");
        c.global().set(&c, &k, &o);

        let name = value::String::from_str(&i, "test.js");
        let source = value::String::from_str(&i, "o.test");
        let script = Script::compile_with_name(&i, &c, &name, &source).unwrap();
        let result = script.run(&c).unwrap();

        assert!(result.is_int32());
        assert_eq!(5, result.int32_value(&c));
    }

    #[test]
    fn run_object_template_instance_function() {
        let i = Isolate::new();
        let c = Context::new(&i);
        let ot = template::ObjectTemplate::new(&i);
        let ft = template::FunctionTemplate::new(&i, &c, Box::new(test_function));
        ot.set("f", &ft);

        let o = ot.new_instance(&c);
        let k = value::String::from_str(&i, "o");
        c.global().set(&c, &k, &o);

        let name = value::String::from_str(&i, "test.js");
        let source = value::String::from_str(&i, "o.f(2, 3)");
        let script = Script::compile_with_name(&i, &c, &name, &source).unwrap();
        let result = script.run(&c).unwrap();

        assert!(result.is_int32());
        assert_eq!(5, result.int32_value(&c));
    }

    #[test]
    fn isolate_rc() {
        let (f, c, p) = {
            let isolate = Isolate::new();
            let context = Context::new(&isolate);
            let param = value::Integer::new(&isolate, 42);

            let function = value::Function::new(&isolate,
                                                &context,
                                                1,
                                                Box::new(|mut info| Ok(info.args.remove(0))));
            (function, context, param)
        };

        let result = f.call(&c, &[&p]).unwrap();
        assert_eq!(42, result.uint32_value(&c));
    }

    #[test]
    #[should_panic]
    fn isolate_exception() {
        let isolate = Isolate::new();
        let context = Context::new(&isolate);

        let closure_isolate = isolate.clone();
        let f = value::Function::new(&isolate,
                &context,
                0,
                Box::new(move |_| {
                    let msg = value::String::from_str(&closure_isolate, "FooBar");
                    let ex = value::Exception::error(&closure_isolate, &msg);
                    Err(ex)
                }));

        let name = value::String::from_str(&isolate, "f");
        context.global().set(&context, &name, &f);

        let source = value::String::from_str(&isolate, "f();");
        let script = Script::compile(&isolate, &context, &source).unwrap();
        script.run(&context).unwrap();
    }

    #[test]
    fn closure_lifetime() {
        struct Foo {
            msg: String,
        }

        let isolate = Isolate::new();
        let context = Context::new(&isolate);

        let f = {
            let foo = Foo { msg: "Hello, World!".into() };

            let closure_isolate = isolate.clone();
            value::Function::new(&isolate,
                                 &context,
                                 0,
                                 Box::new(move |_| {
                                     assert_eq!("Hello, World!", &foo.msg);
                                     Ok(value::undefined(&closure_isolate).into())
                                 }))
        };

        let bar = Foo { msg: "Goodbye, World!".into() };
        let name = value::String::from_str(&isolate, "f");
        context.global().set(&context, &name, &f);

        let source = value::String::from_str(&isolate, "f();");
        let script = Script::compile(&isolate, &context, &source).unwrap();
        let result = script.run(&context).unwrap();

        assert_eq!("Goodbye, World!", bar.msg);
        assert!(result.is_undefined());
    }

    #[test]
    #[should_panic="You dun goofed"]
    fn propagate_panic() {
        let isolate = Isolate::new();
        let context = Context::new(&isolate);

        let f = value::Function::new(&isolate,
                                     &context,
                                     0,
                                     Box::new(|_| panic!("You dun goofed")));

        f.call(&context, &[]).unwrap();
    }

    #[test]
    fn catch_panic() {
        let isolate = Isolate::new();
        let context = Context::new(&isolate);

        let f = value::Function::new(&isolate,
                                     &context,
                                     0,
                                     Box::new(|_| panic!("Something: {}", 42)));

        let f_key = value::String::from_str(&isolate, "f");
        context.global().set(&context, &f_key, &f);

        let source = value::String::from_str(&isolate,
                                             "(function() { try { f(); } catch (e) { return \
                                              e.message; } })()");
        let script = Script::compile(&isolate, &context, &source).unwrap();
        let result = script.run(&context).unwrap();

        let result = result.into_string().unwrap();

        assert_eq!("Rust panic: Something: 42", result.value());
    }
}

#[cfg(all(feature="unstable", test))]
mod benches {
    extern crate test;

    use super::*;

    #[bench]
    fn js_function_call(bencher: &mut test::Bencher) {
        let isolate = Isolate::new();
        let context = Context::new(&isolate);
        let name = value::String::from_str(&isolate, "test.js");
        let source = value::String::from_str(&isolate, "(function(a) { return a; })");
        let script = Script::compile_with_name(&isolate, &context, &name, &source).unwrap();
        let result = script.run(&context).unwrap();

        let function = result.into_function().unwrap();
        let param = value::Integer::new(&isolate, 42);

        bencher.iter(|| function.call(&context, &[&param]).unwrap());
    }

    #[bench]
    fn native_function_call(bencher: &mut test::Bencher) {
        let isolate = Isolate::new();
        let context = Context::new(&isolate);

        let function = value::Function::new(&isolate,
                                            &context,
                                            1,
                                            Box::new(|mut info| Ok(info.args.remove(0))));
        let param = value::Integer::new(&isolate, 42);

        bencher.iter(|| function.call(&context, &[&param]).unwrap());
    }
}