9
9
#include " SandBox.h"
10
10
#include " Buffer.h"
11
11
#include " path.h"
12
+ #include " parse.h"
12
13
#include " options.h"
13
14
#include " ifs/global.h"
15
+ #include " ifs/encoding.h"
14
16
15
17
namespace fibjs {
16
18
19
+ extern std::vector<char *> s_argv;
20
+ static std::vector<exlib::string> script_argv;
21
+
17
22
result_t SandBox::run_main (exlib::string fname, v8::Local<v8::Array> argv)
18
23
{
19
24
result_t hr;
20
25
obj_ptr<Buffer_base> bin;
26
+ int32_t step = 0 ;
27
+
28
+ while (true ) {
29
+ if (fname[0 ] == ' -' && fname[1 ] == ' -' ) {
30
+ int32_t i;
31
+ exlib::string tmp (" opt_tools/" );
32
+ tmp += fname.c_str () + 2 ;
21
33
22
- if (fname[0 ] == ' -' && fname[1 ] == ' -' ) {
23
- int32_t i;
24
- exlib::string tmp (" opt_tools/" );
25
- tmp += fname.c_str () + 2 ;
34
+ for (i = 0 ; opt_tools[i].name && qstrcmp (opt_tools[i].name , tmp.c_str ()); i++)
35
+ ;
36
+ opt_tools[i].getDate (bin);
37
+
38
+ break ;
39
+ }
26
40
27
- for (i = 0 ; opt_tools[i].name && qstrcmp (opt_tools[i].name , tmp.c_str ()); i++)
28
- ;
29
- opt_tools[i].getDate (bin);
30
- } else {
31
41
bool isAbs;
32
42
exlib::string rname;
33
43
@@ -39,17 +49,91 @@ result_t SandBox::run_main(exlib::string fname, v8::Local<v8::Array> argv)
39
49
path_base::normalize (fname, fname);
40
50
41
51
hr = resolveFile (fname, bin, NULL );
42
- if (hr < 0 ) {
43
- if (isAbs)
44
- return hr;
52
+ if (hr >= 0 )
53
+ break ;
45
54
46
- fname = " node_modules/.bin/" + rname;
47
- os_resolve (fname);
55
+ if (isAbs)
56
+ return hr;
57
+
58
+ fname = " node_modules/.bin/" + rname;
59
+ os_resolve (fname);
60
+
61
+ hr = resolveFile (fname, bin, NULL );
62
+ if (hr >= 0 )
63
+ break ;
64
+
65
+ if (step > 0 )
66
+ return CALL_E_FILE_NOT_FOUND;
67
+
68
+ v8::Local<v8::Value> v;
69
+ exlib::string buf;
70
+ Isolate* isolate = holder ();
71
+ v8::Local<v8::Context> context = isolate->context ();
72
+
73
+ hr = loadFile (" package.json" , bin);
74
+ if (hr < 0 )
75
+ return CALL_E_FILE_NOT_FOUND;
76
+
77
+ bin->toString (buf);
78
+ hr = json_base::decode (buf, v);
79
+ if (hr < 0 )
80
+ return hr;
81
+
82
+ if (v.IsEmpty () || !v->IsObject ())
83
+ return CHECK_ERROR (Runtime::setError (" SandBox: Invalid package.json" ));
84
+
85
+ v8::Local<v8::Object> o = v8::Local<v8::Object>::Cast (v);
86
+ v8::Local<v8::Value> scripts = o->Get (context, isolate->NewString (" scripts" , 7 )).FromMaybe (v8::Local<v8::Value>());
87
+ if (IsEmpty (scripts) || !scripts->IsObject ())
88
+ return CALL_E_FILE_NOT_FOUND;
48
89
49
- hr = resolveFile (fname, bin, NULL );
50
- if (hr < 0 )
51
- return hr;
90
+ o = v8::Local<v8::Object>::Cast (scripts);
91
+
92
+ v8::Local<v8::Value> cmd = o->Get (context, isolate->NewString (rname)).FromMaybe (v8::Local<v8::Value>());
93
+ if (IsEmpty (cmd) || !cmd->IsString ())
94
+ return CALL_E_FILE_NOT_FOUND;
95
+
96
+ exlib::string cmd_str = isolate->toString (cmd);
97
+ fname.clear ();
98
+
99
+ _parser p (cmd_str.c_str (), (int32_t )cmd_str.length ());
100
+
101
+ p.skipSpace ();
102
+
103
+ char ch = p.get ();
104
+ if (ch == ' \' ' || ch == ' \" ' ) {
105
+ p.skip ();
106
+ p.getString (fname, ch);
107
+ p.skip ();
108
+ } else
109
+ p.getWord (fname);
110
+
111
+ script_argv.clear ();
112
+
113
+ while (true ) {
114
+ exlib::string arg;
115
+
116
+ p.skipSpace ();
117
+
118
+ char ch = p.get ();
119
+ if (ch == ' \' ' || ch == ' \" ' ) {
120
+ p.skip ();
121
+ p.getString (arg, ch);
122
+ p.skip ();
123
+ } else
124
+ p.getWord (arg);
125
+
126
+ if (arg.empty ())
127
+ break ;
128
+
129
+ script_argv.push_back (arg);
52
130
}
131
+
132
+ s_argv.resize (2 );
133
+ for (size_t i = 0 ; i < script_argv.size (); i++)
134
+ s_argv.push_back ((char *)script_argv[i].c_str ());
135
+
136
+ step++;
53
137
}
54
138
55
139
obj_ptr<ExtLoader> l;
0 commit comments