blob: 05c29d7107eed6ad4ceea59d1b91de396ec2b1cc [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"
37#include "top.h"
38#include "cctest.h"
39
40using namespace v8::internal;
41
42static v8::Persistent<v8::Context> env;
43
44// --- 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++)
Leon Clarked91b9f72010-01-27 17:25:45 +000077 printf("%lc", static_cast<wint_t>(string[j]));
Steve Blocka7e24c12009-10-30 11:49:00 +000078 DeleteArray(string);
79 }
80 printf("\n");
81 return v8::Undefined();
82}
83
84
85static PrintExtension kPrintExtension;
86v8::DeclareExtension kPrintExtensionDeclaration(&kPrintExtension);
87
88
89static 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
101static Object* GetGlobalProperty(const char* name) {
102 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)));
117 Handle<JSFunction> boilerplate =
118 Compiler::Compile(source_code, Handle<String>(), 0, 0, NULL, NULL);
119 return Factory::NewFunctionFromBoilerplate(boilerplate,
120 Top::global_context());
121}
122
123
124static double Inc(int x) {
125 const char* source = "result = %d + 1;";
126 EmbeddedVector<char, 512> buffer;
127 OS::SNPrintF(buffer, source, x);
128
129 Handle<JSFunction> fun = Compile(buffer.start());
130 if (fun.is_null()) return -1;
131
132 bool has_pending_exception;
133 Handle<JSObject> global(Top::context()->global());
134 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
135 CHECK(!has_pending_exception);
136 return GetGlobalProperty("result")->Number();
137}
138
139
140TEST(Inc) {
141 InitializeVM();
142 v8::HandleScope scope;
143 CHECK_EQ(4.0, Inc(3));
144}
145
146
147static double Add(int x, int y) {
148 Handle<JSFunction> fun = Compile("result = x + y;");
149 if (fun.is_null()) return -1;
150
151 SetGlobalProperty("x", Smi::FromInt(x));
152 SetGlobalProperty("y", Smi::FromInt(y));
153 bool has_pending_exception;
154 Handle<JSObject> global(Top::context()->global());
155 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
156 CHECK(!has_pending_exception);
157 return GetGlobalProperty("result")->Number();
158}
159
160
161TEST(Add) {
162 InitializeVM();
163 v8::HandleScope scope;
164 CHECK_EQ(5.0, Add(2, 3));
165}
166
167
168static double Abs(int x) {
169 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;");
170 if (fun.is_null()) return -1;
171
172 SetGlobalProperty("x", Smi::FromInt(x));
173 bool has_pending_exception;
174 Handle<JSObject> global(Top::context()->global());
175 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
176 CHECK(!has_pending_exception);
177 return GetGlobalProperty("result")->Number();
178}
179
180
181TEST(Abs) {
182 InitializeVM();
183 v8::HandleScope scope;
184 CHECK_EQ(3.0, Abs(-3));
185}
186
187
188static double Sum(int n) {
189 Handle<JSFunction> fun =
190 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;");
191 if (fun.is_null()) return -1;
192
193 SetGlobalProperty("n", Smi::FromInt(n));
194 bool has_pending_exception;
195 Handle<JSObject> global(Top::context()->global());
196 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
197 CHECK(!has_pending_exception);
198 return GetGlobalProperty("result")->Number();
199}
200
201
202TEST(Sum) {
203 InitializeVM();
204 v8::HandleScope scope;
205 CHECK_EQ(5050.0, Sum(100));
206}
207
208
209TEST(Print) {
210 InitializeVM();
211 v8::HandleScope scope;
212 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);";
213 Handle<JSFunction> fun = Compile(source);
214 if (fun.is_null()) return;
215 bool has_pending_exception;
216 Handle<JSObject> global(Top::context()->global());
217 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
218 CHECK(!has_pending_exception);
219}
220
221
222// The following test method stems from my coding efforts today. It
223// tests all the functionality I have added to the compiler today
224TEST(Stuff) {
225 InitializeVM();
226 v8::HandleScope scope;
227 const char* source =
228 "r = 0;\n"
229 "a = new Object;\n"
230 "if (a == a) r+=1;\n" // 1
231 "if (a != new Object()) r+=2;\n" // 2
232 "a.x = 42;\n"
233 "if (a.x == 42) r+=4;\n" // 4
234 "function foo() { var x = 87; return x; }\n"
235 "if (foo() == 87) r+=8;\n" // 8
236 "function bar() { var x; x = 99; return x; }\n"
237 "if (bar() == 99) r+=16;\n" // 16
238 "function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n"
239 "if (baz() == 6) r+=32;\n" // 32
240 "function Cons0() { this.x = 42; this.y = 87; }\n"
241 "if (new Cons0().x == 42) r+=64;\n" // 64
242 "if (new Cons0().y == 87) r+=128;\n" // 128
243 "function Cons2(x, y) { this.sum = x + y; }\n"
244 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256
245
246 Handle<JSFunction> fun = Compile(source);
247 CHECK(!fun.is_null());
248 bool has_pending_exception;
249 Handle<JSObject> global(Top::context()->global());
250 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
251 CHECK(!has_pending_exception);
252 CHECK_EQ(511.0, GetGlobalProperty("r")->Number());
253}
254
255
256TEST(UncaughtThrow) {
257 InitializeVM();
258 v8::HandleScope scope;
259
260 const char* source = "throw 42;";
261 Handle<JSFunction> fun = Compile(source);
262 CHECK(!fun.is_null());
263 bool has_pending_exception;
264 Handle<JSObject> global(Top::context()->global());
265 Handle<Object> result =
266 Execution::Call(fun, global, 0, NULL, &has_pending_exception);
267 CHECK(has_pending_exception);
268 CHECK_EQ(42.0, Top::pending_exception()->Number());
269}
270
271
272// Tests calling a builtin function from C/C++ code, and the builtin function
273// performs GC. It creates a stack frame looks like following:
274// | C (PerformGC) |
275// | JS-to-C |
276// | JS |
277// | C-to-JS |
278TEST(C2JSFrames) {
279 InitializeVM();
280 v8::HandleScope scope;
281
282 const char* source = "function foo(a) { gc(), print(a); }";
283
284 Handle<JSFunction> fun0 = Compile(source);
285 CHECK(!fun0.is_null());
286
287 // Run the generated code to populate the global object with 'foo'.
288 bool has_pending_exception;
289 Handle<JSObject> global(Top::context()->global());
290 Execution::Call(fun0, global, 0, NULL, &has_pending_exception);
291 CHECK(!has_pending_exception);
292
293 Handle<Object> fun1 =
294 Handle<Object>(
295 Top::context()->global()->GetProperty(
296 *Factory::LookupAsciiSymbol("foo")));
297 CHECK(fun1->IsJSFunction());
298
299 Object** argv[1] = {
300 Handle<Object>::cast(Factory::LookupAsciiSymbol("hello")).location()
301 };
302 Execution::Call(Handle<JSFunction>::cast(fun1), global, 1, argv,
303 &has_pending_exception);
304 CHECK(!has_pending_exception);
305}
306
307
308// Regression 236. Calling InitLineEnds on a Script with undefined
309// source resulted in crash.
310TEST(Regression236) {
311 InitializeVM();
312 v8::HandleScope scope;
313
314 Handle<Script> script = Factory::NewScript(Factory::empty_string());
315 script->set_source(Heap::undefined_value());
316 CHECK_EQ(-1, GetScriptLineNumber(script, 0));
317 CHECK_EQ(-1, GetScriptLineNumber(script, 100));
318 CHECK_EQ(-1, GetScriptLineNumber(script, -1));
319}