blob: d3dd9c6ff0e97b715604bedfc33026138ea46101 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// 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>
Leon Clarked91b9f72010-01-27 17:25:45 +000029#include <wchar.h> // wint_t
Steve Blocka7e24c12009-10-30 11:49:00 +000030
31#include "v8.h"
32
33#include "compiler.h"
34#include "execution.h"
35#include "factory.h"
36#include "platform.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000037#include "cctest.h"
38
39using namespace v8::internal;
40
41static v8::Persistent<v8::Context> env;
42
43// --- P r i n t E x t e n s i o n ---
44
45class PrintExtension : public v8::Extension {
46 public:
47 PrintExtension() : v8::Extension("v8/print", kSource) { }
48 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
49 v8::Handle<v8::String> name);
50 static v8::Handle<v8::Value> Print(const v8::Arguments& args);
51 private:
52 static const char* kSource;
53};
54
55
56const char* PrintExtension::kSource = "native function print();";
57
58
59v8::Handle<v8::FunctionTemplate> PrintExtension::GetNativeFunction(
60 v8::Handle<v8::String> str) {
61 return v8::FunctionTemplate::New(PrintExtension::Print);
62}
63
64
65v8::Handle<v8::Value> PrintExtension::Print(const v8::Arguments& args) {
66 for (int i = 0; i < args.Length(); i++) {
67 if (i != 0) printf(" ");
68 v8::HandleScope scope;
69 v8::Handle<v8::Value> arg = args[i];
70 v8::Handle<v8::String> string_obj = arg->ToString();
71 if (string_obj.IsEmpty()) return string_obj;
72 int length = string_obj->Length();
73 uint16_t* string = NewArray<uint16_t>(length + 1);
74 string_obj->Write(string);
75 for (int j = 0; j < length; j++)
Leon Clarked91b9f72010-01-27 17:25:45 +000076 printf("%lc", static_cast<wint_t>(string[j]));
Steve Blocka7e24c12009-10-30 11:49:00 +000077 DeleteArray(string);
78 }
79 printf("\n");
80 return v8::Undefined();
81}
82
83
84static PrintExtension kPrintExtension;
85v8::DeclareExtension kPrintExtensionDeclaration(&kPrintExtension);
86
87
88static void InitializeVM() {
89 if (env.IsEmpty()) {
90 v8::HandleScope scope;
91 const char* extensions[] = { "v8/print", "v8/gc" };
92 v8::ExtensionConfiguration config(2, extensions);
93 env = v8::Context::New(&config);
94 }
95 v8::HandleScope scope;
96 env->Enter();
97}
98
99
John Reck59135872010-11-02 12:39:01 -0700100static MaybeObject* GetGlobalProperty(const char* name) {
Steve Block44f0eee2011-05-26 01:26:41 +0100101 Handle<String> symbol = FACTORY->LookupAsciiSymbol(name);
102 return Isolate::Current()->context()->global()->GetProperty(*symbol);
Steve Blocka7e24c12009-10-30 11:49:00 +0000103}
104
105
106static void SetGlobalProperty(const char* name, Object* value) {
107 Handle<Object> object(value);
Steve Block44f0eee2011-05-26 01:26:41 +0100108 Handle<String> symbol = FACTORY->LookupAsciiSymbol(name);
109 Handle<JSObject> global(Isolate::Current()->context()->global());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100110 SetProperty(global, symbol, object, NONE, kNonStrictMode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000111}
112
113
114static Handle<JSFunction> Compile(const char* source) {
Steve Block44f0eee2011-05-26 01:26:41 +0100115 Handle<String> source_code(FACTORY->NewStringFromUtf8(CStrVector(source)));
Steve Block6ded16b2010-05-10 14:33:55 +0100116 Handle<SharedFunctionInfo> shared_function =
117 Compiler::Compile(source_code,
118 Handle<String>(),
119 0,
120 0,
121 NULL,
122 NULL,
123 Handle<String>::null(),
124 NOT_NATIVES_CODE);
Steve Block44f0eee2011-05-26 01:26:41 +0100125 return FACTORY->NewFunctionFromSharedFunctionInfo(shared_function,
126 Isolate::Current()->global_context());
Steve Blocka7e24c12009-10-30 11:49:00 +0000127}
128
129
130static double Inc(int x) {
131 const char* source = "result = %d + 1;";
132 EmbeddedVector<char, 512> buffer;
133 OS::SNPrintF(buffer, source, x);
134
135 Handle<JSFunction> fun = Compile(buffer.start());
136 if (fun.is_null()) return -1;
137
138 bool has_pending_exception;
Steve Block44f0eee2011-05-26 01:26:41 +0100139 Handle<JSObject> global(Isolate::Current()->context()->global());
Steve Blocka7e24c12009-10-30 11:49:00 +0000140 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
141 CHECK(!has_pending_exception);
John Reck59135872010-11-02 12:39:01 -0700142 return GetGlobalProperty("result")->ToObjectChecked()->Number();
Steve Blocka7e24c12009-10-30 11:49:00 +0000143}
144
145
146TEST(Inc) {
147 InitializeVM();
148 v8::HandleScope scope;
149 CHECK_EQ(4.0, Inc(3));
150}
151
152
153static double Add(int x, int y) {
154 Handle<JSFunction> fun = Compile("result = x + y;");
155 if (fun.is_null()) return -1;
156
157 SetGlobalProperty("x", Smi::FromInt(x));
158 SetGlobalProperty("y", Smi::FromInt(y));
159 bool has_pending_exception;
Steve Block44f0eee2011-05-26 01:26:41 +0100160 Handle<JSObject> global(Isolate::Current()->context()->global());
Steve Blocka7e24c12009-10-30 11:49:00 +0000161 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
162 CHECK(!has_pending_exception);
John Reck59135872010-11-02 12:39:01 -0700163 return GetGlobalProperty("result")->ToObjectChecked()->Number();
Steve Blocka7e24c12009-10-30 11:49:00 +0000164}
165
166
167TEST(Add) {
168 InitializeVM();
169 v8::HandleScope scope;
170 CHECK_EQ(5.0, Add(2, 3));
171}
172
173
174static double Abs(int x) {
175 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;");
176 if (fun.is_null()) return -1;
177
178 SetGlobalProperty("x", Smi::FromInt(x));
179 bool has_pending_exception;
Steve Block44f0eee2011-05-26 01:26:41 +0100180 Handle<JSObject> global(Isolate::Current()->context()->global());
Steve Blocka7e24c12009-10-30 11:49:00 +0000181 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
182 CHECK(!has_pending_exception);
John Reck59135872010-11-02 12:39:01 -0700183 return GetGlobalProperty("result")->ToObjectChecked()->Number();
Steve Blocka7e24c12009-10-30 11:49:00 +0000184}
185
186
187TEST(Abs) {
188 InitializeVM();
189 v8::HandleScope scope;
190 CHECK_EQ(3.0, Abs(-3));
191}
192
193
194static double Sum(int n) {
195 Handle<JSFunction> fun =
196 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;");
197 if (fun.is_null()) return -1;
198
199 SetGlobalProperty("n", Smi::FromInt(n));
200 bool has_pending_exception;
Steve Block44f0eee2011-05-26 01:26:41 +0100201 Handle<JSObject> global(Isolate::Current()->context()->global());
Steve Blocka7e24c12009-10-30 11:49:00 +0000202 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
203 CHECK(!has_pending_exception);
John Reck59135872010-11-02 12:39:01 -0700204 return GetGlobalProperty("result")->ToObjectChecked()->Number();
Steve Blocka7e24c12009-10-30 11:49:00 +0000205}
206
207
208TEST(Sum) {
209 InitializeVM();
210 v8::HandleScope scope;
211 CHECK_EQ(5050.0, Sum(100));
212}
213
214
215TEST(Print) {
216 InitializeVM();
217 v8::HandleScope scope;
218 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);";
219 Handle<JSFunction> fun = Compile(source);
220 if (fun.is_null()) return;
221 bool has_pending_exception;
Steve Block44f0eee2011-05-26 01:26:41 +0100222 Handle<JSObject> global(Isolate::Current()->context()->global());
Steve Blocka7e24c12009-10-30 11:49:00 +0000223 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
224 CHECK(!has_pending_exception);
225}
226
227
228// The following test method stems from my coding efforts today. It
229// tests all the functionality I have added to the compiler today
230TEST(Stuff) {
231 InitializeVM();
232 v8::HandleScope scope;
233 const char* source =
234 "r = 0;\n"
235 "a = new Object;\n"
236 "if (a == a) r+=1;\n" // 1
237 "if (a != new Object()) r+=2;\n" // 2
238 "a.x = 42;\n"
239 "if (a.x == 42) r+=4;\n" // 4
240 "function foo() { var x = 87; return x; }\n"
241 "if (foo() == 87) r+=8;\n" // 8
242 "function bar() { var x; x = 99; return x; }\n"
243 "if (bar() == 99) r+=16;\n" // 16
244 "function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n"
245 "if (baz() == 6) r+=32;\n" // 32
246 "function Cons0() { this.x = 42; this.y = 87; }\n"
247 "if (new Cons0().x == 42) r+=64;\n" // 64
248 "if (new Cons0().y == 87) r+=128;\n" // 128
249 "function Cons2(x, y) { this.sum = x + y; }\n"
250 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256
251
252 Handle<JSFunction> fun = Compile(source);
253 CHECK(!fun.is_null());
254 bool has_pending_exception;
Steve Block44f0eee2011-05-26 01:26:41 +0100255 Handle<JSObject> global(Isolate::Current()->context()->global());
Steve Blocka7e24c12009-10-30 11:49:00 +0000256 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
257 CHECK(!has_pending_exception);
John Reck59135872010-11-02 12:39:01 -0700258 CHECK_EQ(511.0, GetGlobalProperty("r")->ToObjectChecked()->Number());
Steve Blocka7e24c12009-10-30 11:49:00 +0000259}
260
261
262TEST(UncaughtThrow) {
263 InitializeVM();
264 v8::HandleScope scope;
265
266 const char* source = "throw 42;";
267 Handle<JSFunction> fun = Compile(source);
268 CHECK(!fun.is_null());
269 bool has_pending_exception;
Steve Block44f0eee2011-05-26 01:26:41 +0100270 Handle<JSObject> global(Isolate::Current()->context()->global());
Steve Blocka7e24c12009-10-30 11:49:00 +0000271 Handle<Object> result =
272 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
273 CHECK(has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +0100274 CHECK_EQ(42.0, Isolate::Current()->pending_exception()->
275 ToObjectChecked()->Number());
Steve Blocka7e24c12009-10-30 11:49:00 +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;
Steve Block44f0eee2011-05-26 01:26:41 +0100296 Handle<JSObject> global(Isolate::Current()->context()->global());
Steve Blocka7e24c12009-10-30 11:49:00 +0000297 Execution::Call(fun0, global, 0, NULL, &has_pending_exception);
298 CHECK(!has_pending_exception);
299
Steve Block44f0eee2011-05-26 01:26:41 +0100300 Object* foo_symbol = FACTORY->LookupAsciiSymbol("foo")->ToObjectChecked();
301 MaybeObject* fun1_object = Isolate::Current()->context()->global()->
302 GetProperty(String::cast(foo_symbol));
John Reck59135872010-11-02 12:39:01 -0700303 Handle<Object> fun1(fun1_object->ToObjectChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 CHECK(fun1->IsJSFunction());
305
306 Object** argv[1] = {
Steve Block44f0eee2011-05-26 01:26:41 +0100307 Handle<Object>::cast(FACTORY->LookupAsciiSymbol("hello")).location()
Steve Blocka7e24c12009-10-30 11:49:00 +0000308 };
309 Execution::Call(Handle<JSFunction>::cast(fun1), global, 1, argv,
310 &has_pending_exception);
311 CHECK(!has_pending_exception);
312}
313
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
Steve Block44f0eee2011-05-26 01:26:41 +0100321 Handle<Script> script = FACTORY->NewScript(FACTORY->empty_string());
322 script->set_source(HEAP->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000323 CHECK_EQ(-1, GetScriptLineNumber(script, 0));
324 CHECK_EQ(-1, GetScriptLineNumber(script, 100));
325 CHECK_EQ(-1, GetScriptLineNumber(script, -1));
326}
Andrei Popescu402d9372010-02-26 13:31:12 +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}