1 // Copyright 2020 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 extern "C" {
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 #include "libreprl.h"
12 
13 struct reprl_context* ctx;
14 
execute(const char * code)15 int execute(const char* code) {
16   uint64_t exec_time;
17   return reprl_execute(ctx, code, strlen(code), 1000, &exec_time, 0);
18 }
19 
expect_success(const char * code)20 void expect_success(const char* code) {
21   if (execute(code) != 0) {
22     printf("Execution of \"%s\" failed\n", code);
23     exit(1);
24   }
25 }
26 
expect_failure(const char * code)27 void expect_failure(const char* code) {
28   if (execute(code) == 0) {
29     printf("Execution of \"%s\" unexpectedly succeeded\n", code);
30     exit(1);
31   }
32 }
33 
main(int argc,char ** argv)34 int main(int argc, char** argv) {
35   ctx = reprl_create_context();
36 
37   const char* env[] = {nullptr};
38   const char* prog = argc > 1 ? argv[1] : "./out.gn/x64.debug/d8";
39   const char* args[] = {prog, nullptr};
40   if (reprl_initialize_context(ctx, args, env, 1, 1) != 0) {
41     printf("REPRL initialization failed\n");
42     return -1;
43   }
44 
45   // Basic functionality test
46   if (execute("let greeting = \"Hello World!\";") != 0) {
47     printf(
48         "Script execution failed, is %s the path to d8 built with "
49         "v8_fuzzilli=true?\n",
50         prog);
51     return -1;
52   }
53 
54   // Verify that runtime exceptions can be detected
55   expect_failure("throw 'failure';");
56 
57   // Verify that existing state is property reset between executions
58   expect_success("globalProp = 42; Object.prototype.foo = \"bar\";");
59   expect_success("if (typeof(globalProp) !== 'undefined') throw 'failure'");
60   expect_success("if (typeof(({}).foo) !== 'undefined') throw 'failure'");
61 
62   // Verify that rejected promises are properly reset between executions
63   expect_failure("async function fail() { throw 42; }; fail()");
64   expect_success("42");
65   expect_failure("async function fail() { throw 42; }; fail()");
66 
67   puts("OK");
68   return 0;
69 }
70 }
71