blob: 38b22f9b1bf135777b7764b507998da2cf725509 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2010 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// Adapted from test/mjsunit/compiler/variables.js
29
30#include <limits.h>
31
32#include "src/v8.h"
33
34#include "src/api.h"
35#include "src/base/platform/platform.h"
36#include "src/compilation-cache.h"
37#include "src/execution.h"
38#include "src/isolate.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000039#include "src/parsing/parser.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040#include "src/unicode-inl.h"
41#include "src/utils.h"
42#include "test/cctest/cctest.h"
43
44using ::v8::Context;
45using ::v8::Extension;
46using ::v8::Function;
47using ::v8::FunctionTemplate;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048using ::v8::HandleScope;
49using ::v8::Local;
50using ::v8::Message;
51using ::v8::MessageCallback;
52using ::v8::Object;
53using ::v8::ObjectTemplate;
54using ::v8::Persistent;
55using ::v8::Script;
56using ::v8::StackTrace;
57using ::v8::String;
58using ::v8::TryCatch;
59using ::v8::Undefined;
60using ::v8::V8;
61using ::v8::Value;
62
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000063static void ExpectInt32(Local<Context> context, int32_t expected,
64 Local<Value> result) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065 CHECK(result->IsInt32());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066 CHECK_EQ(expected, result->Int32Value(context).FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067}
68
69
70// Global variables.
71TEST(global_variables) {
72 LocalContext env;
73 v8::HandleScope scope(env->GetIsolate());
74 Local<Value> result = CompileRun(
75"var x = 0;"
76"function f0() { return x; }"
77"f0();");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000078 ExpectInt32(env.local(), 0, result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079}
80
81
82// Parameters.
83TEST(parameters) {
84 LocalContext env;
85 v8::HandleScope scope(env->GetIsolate());
86 Local<Value> result = CompileRun(
87"function f1(x) { return x; }"
88"f1(1);");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000089 ExpectInt32(env.local(), 1, result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090}
91
92
93// Stack-allocated locals.
94TEST(stack_allocated_locals) {
95 LocalContext env;
96 v8::HandleScope scope(env->GetIsolate());
97 Local<Value> result = CompileRun(
98"function f2() { var x = 2; return x; }"
99"f2();");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000100 ExpectInt32(env.local(), 2, result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101}
102
103
104// Context-allocated locals. Local function forces x into f3's context.
105TEST(context_allocated_locals) {
106 LocalContext env;
107 v8::HandleScope scope(env->GetIsolate());
108 Local<Value> result = CompileRun(
109"function f3(x) {"
110" function g() { return x; }"
111" return x;"
112"}"
113"f3(3);");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000114 ExpectInt32(env.local(), 3, result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115}
116
117
118// Local function reads x from an outer context.
119TEST(read_from_outer_context) {
120 LocalContext env;
121 v8::HandleScope scope(env->GetIsolate());
122 Local<Value> result = CompileRun(
123"function f4(x) {"
124" function g() { return x; }"
125" return g();"
126"}"
127"f4(4);");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000128 ExpectInt32(env.local(), 4, result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129}
130
131
132// Local function reads x from an outer context.
133TEST(lookup_slots) {
134 LocalContext env;
135 v8::HandleScope scope(env->GetIsolate());
136 Local<Value> result = CompileRun(
137"function f5(x) {"
138" with ({}) return x;"
139"}"
140"f5(5);");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000141 ExpectInt32(env.local(), 5, result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000142}