blob: b424b7f9e1483571ac7c6f9abd76572c50d1c390 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include <stdlib.h>
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000029#include <wchar.h> // wint_t
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000030
31#include "v8.h"
32
33#include "compiler.h"
34#include "execution.h"
35#include "factory.h"
36#include "platform.h"
37#include "top.h"
38#include "cctest.h"
39
40using namespace v8::internal;
41
42static v8::Persistent<v8::Context> env;
43
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000044// --- P r i n t E x t e n s i o n ---
45
46class PrintExtension : public v8::Extension {
47 public:
48 PrintExtension() : v8::Extension("v8/print", kSource) { }
49 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
50 v8::Handle<v8::String> name);
51 static v8::Handle<v8::Value> Print(const v8::Arguments& args);
52 private:
53 static const char* kSource;
54};
55
56
57const char* PrintExtension::kSource = "native function print();";
58
59
60v8::Handle<v8::FunctionTemplate> PrintExtension::GetNativeFunction(
61 v8::Handle<v8::String> str) {
62 return v8::FunctionTemplate::New(PrintExtension::Print);
63}
64
65
66v8::Handle<v8::Value> PrintExtension::Print(const v8::Arguments& args) {
67 for (int i = 0; i < args.Length(); i++) {
68 if (i != 0) printf(" ");
69 v8::HandleScope scope;
70 v8::Handle<v8::Value> arg = args[i];
71 v8::Handle<v8::String> string_obj = arg->ToString();
72 if (string_obj.IsEmpty()) return string_obj;
73 int length = string_obj->Length();
74 uint16_t* string = NewArray<uint16_t>(length + 1);
75 string_obj->Write(string);
76 for (int j = 0; j < length; j++)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000077 printf("%lc", static_cast<wint_t>(string[j]));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000078 DeleteArray(string);
79 }
80 printf("\n");
81 return v8::Undefined();
82}
83
84
85static PrintExtension kPrintExtension;
86v8::DeclareExtension kPrintExtensionDeclaration(&kPrintExtension);
87
88
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000089static void InitializeVM() {
90 if (env.IsEmpty()) {
91 v8::HandleScope scope;
92 const char* extensions[] = { "v8/print", "v8/gc" };
93 v8::ExtensionConfiguration config(2, extensions);
94 env = v8::Context::New(&config);
95 }
96 v8::HandleScope scope;
97 env->Enter();
98}
99
100
lrn@chromium.org303ada72010-10-27 09:33:13 +0000101static MaybeObject* GetGlobalProperty(const char* name) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000102 Handle<String> symbol = Factory::LookupAsciiSymbol(name);
103 return Top::context()->global()->GetProperty(*symbol);
104}
105
106
107static void SetGlobalProperty(const char* name, Object* value) {
108 Handle<Object> object(value);
109 Handle<String> symbol = Factory::LookupAsciiSymbol(name);
110 Handle<JSObject> global(Top::context()->global());
111 SetProperty(global, symbol, object, NONE);
112}
113
114
115static Handle<JSFunction> Compile(const char* source) {
116 Handle<String> source_code(Factory::NewStringFromUtf8(CStrVector(source)));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000117 Handle<SharedFunctionInfo> shared_function =
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000118 Compiler::Compile(source_code,
119 Handle<String>(),
120 0,
121 0,
122 NULL,
123 NULL,
124 Handle<String>::null(),
125 NOT_NATIVES_CODE);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000126 return Factory::NewFunctionFromSharedFunctionInfo(shared_function,
127 Top::global_context());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000128}
129
130
131static double Inc(int x) {
132 const char* source = "result = %d + 1;";
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000133 EmbeddedVector<char, 512> buffer;
134 OS::SNPrintF(buffer, source, x);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000135
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000136 Handle<JSFunction> fun = Compile(buffer.start());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000137 if (fun.is_null()) return -1;
138
139 bool has_pending_exception;
140 Handle<JSObject> global(Top::context()->global());
141 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
142 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000143 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000144}
145
146
147TEST(Inc) {
148 InitializeVM();
149 v8::HandleScope scope;
150 CHECK_EQ(4.0, Inc(3));
151}
152
153
154static double Add(int x, int y) {
155 Handle<JSFunction> fun = Compile("result = x + y;");
156 if (fun.is_null()) return -1;
157
158 SetGlobalProperty("x", Smi::FromInt(x));
159 SetGlobalProperty("y", Smi::FromInt(y));
160 bool has_pending_exception;
161 Handle<JSObject> global(Top::context()->global());
162 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
163 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000164 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000165}
166
167
168TEST(Add) {
169 InitializeVM();
170 v8::HandleScope scope;
171 CHECK_EQ(5.0, Add(2, 3));
172}
173
174
175static double Abs(int x) {
176 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;");
177 if (fun.is_null()) return -1;
178
179 SetGlobalProperty("x", Smi::FromInt(x));
180 bool has_pending_exception;
181 Handle<JSObject> global(Top::context()->global());
182 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
183 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000184 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000185}
186
187
188TEST(Abs) {
189 InitializeVM();
190 v8::HandleScope scope;
191 CHECK_EQ(3.0, Abs(-3));
192}
193
194
195static double Sum(int n) {
196 Handle<JSFunction> fun =
197 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;");
198 if (fun.is_null()) return -1;
199
200 SetGlobalProperty("n", Smi::FromInt(n));
201 bool has_pending_exception;
202 Handle<JSObject> global(Top::context()->global());
203 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
204 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000205 return GetGlobalProperty("result")->ToObjectChecked()->Number();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000206}
207
208
209TEST(Sum) {
210 InitializeVM();
211 v8::HandleScope scope;
212 CHECK_EQ(5050.0, Sum(100));
213}
214
215
216TEST(Print) {
217 InitializeVM();
218 v8::HandleScope scope;
219 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);";
220 Handle<JSFunction> fun = Compile(source);
221 if (fun.is_null()) return;
222 bool has_pending_exception;
223 Handle<JSObject> global(Top::context()->global());
224 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
225 CHECK(!has_pending_exception);
226}
227
228
229// The following test method stems from my coding efforts today. It
230// tests all the functionality I have added to the compiler today
231TEST(Stuff) {
232 InitializeVM();
233 v8::HandleScope scope;
234 const char* source =
235 "r = 0;\n"
236 "a = new Object;\n"
237 "if (a == a) r+=1;\n" // 1
238 "if (a != new Object()) r+=2;\n" // 2
239 "a.x = 42;\n"
240 "if (a.x == 42) r+=4;\n" // 4
241 "function foo() { var x = 87; return x; }\n"
242 "if (foo() == 87) r+=8;\n" // 8
243 "function bar() { var x; x = 99; return x; }\n"
244 "if (bar() == 99) r+=16;\n" // 16
245 "function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n"
246 "if (baz() == 6) r+=32;\n" // 32
247 "function Cons0() { this.x = 42; this.y = 87; }\n"
248 "if (new Cons0().x == 42) r+=64;\n" // 64
249 "if (new Cons0().y == 87) r+=128;\n" // 128
250 "function Cons2(x, y) { this.sum = x + y; }\n"
251 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256
252
253 Handle<JSFunction> fun = Compile(source);
254 CHECK(!fun.is_null());
255 bool has_pending_exception;
256 Handle<JSObject> global(Top::context()->global());
257 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
258 CHECK(!has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000259 CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000260}
261
262
263TEST(UncaughtThrow) {
264 InitializeVM();
265 v8::HandleScope scope;
266
267 const char* source = "throw 42;";
268 Handle<JSFunction> fun = Compile(source);
269 CHECK(!fun.is_null());
270 bool has_pending_exception;
271 Handle<JSObject> global(Top::context()->global());
272 Handle<Object> result =
273 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
274 CHECK(has_pending_exception);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000275 CHECK_EQ(42.0, Top::pending_exception()->ToObjectChecked()->Number());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000276}
277
278
279// Tests calling a builtin function from C/C++ code, and the builtin function
280// performs GC. It creates a stack frame looks like following:
281// | C (PerformGC) |
282// | JS-to-C |
283// | JS |
284// | C-to-JS |
285TEST(C2JSFrames) {
286 InitializeVM();
287 v8::HandleScope scope;
288
289 const char* source = "function foo(a) { gc(), print(a); }";
290
291 Handle<JSFunction> fun0 = Compile(source);
292 CHECK(!fun0.is_null());
293
294 // Run the generated code to populate the global object with 'foo'.
295 bool has_pending_exception;
296 Handle<JSObject> global(Top::context()->global());
297 Execution::Call(fun0, global, 0, NULL, &has_pending_exception);
298 CHECK(!has_pending_exception);
299
lrn@chromium.org303ada72010-10-27 09:33:13 +0000300 Object* foo_symbol = Factory::LookupAsciiSymbol("foo")->ToObjectChecked();
301 MaybeObject* fun1_object =
302 Top::context()->global()->GetProperty(String::cast(foo_symbol));
303 Handle<Object> fun1(fun1_object->ToObjectChecked());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000304 CHECK(fun1->IsJSFunction());
305
306 Object** argv[1] = {
307 Handle<Object>::cast(Factory::LookupAsciiSymbol("hello")).location()
308 };
309 Execution::Call(Handle<JSFunction>::cast(fun1), global, 1, argv,
310 &has_pending_exception);
311 CHECK(!has_pending_exception);
312}
ager@chromium.org381abbb2009-02-25 13:23:22 +0000313
314
315// Regression 236. Calling InitLineEnds on a Script with undefined
316// source resulted in crash.
317TEST(Regression236) {
318 InitializeVM();
319 v8::HandleScope scope;
320
321 Handle<Script> script = Factory::NewScript(Factory::empty_string());
322 script->set_source(Heap::undefined_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000323 CHECK_EQ(-1, GetScriptLineNumber(script, 0));
324 CHECK_EQ(-1, GetScriptLineNumber(script, 100));
325 CHECK_EQ(-1, GetScriptLineNumber(script, -1));
ager@chromium.org381abbb2009-02-25 13:23:22 +0000326}
ager@chromium.org5c838252010-02-19 08:53:10 +0000327
328
329TEST(GetScriptLineNumber) {
330 LocalContext env;
331 v8::HandleScope scope;
332 v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test"));
333 const char function_f[] = "function f() {}";
334 const int max_rows = 1000;
335 const int buffer_size = max_rows + sizeof(function_f);
336 ScopedVector<char> buffer(buffer_size);
337 memset(buffer.start(), '\n', buffer_size - 1);
338 buffer[buffer_size - 1] = '\0';
339
340 for (int i = 0; i < max_rows; ++i) {
341 if (i > 0)
342 buffer[i - 1] = '\n';
343 memcpy(&buffer[i], function_f, sizeof(function_f) - 1);
344 v8::Handle<v8::String> script_body = v8::String::New(buffer.start());
345 v8::Script::Compile(script_body, &origin)->Run();
346 v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(
347 env->Global()->Get(v8::String::New("f")));
348 CHECK_EQ(i, f->GetScriptLineNumber());
349 }
350}