Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(391)

Side by Side Diff: src/compiler.cc

Issue 549207: Added validating JSON parser mode to parser. (Closed)
Patch Set: Created 10 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/ast.cc ('k') | src/json-delay.js » ('j') | src/parser.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 the V8 project authors. All rights reserved. 1 // Copyright 2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 if (checker.has_supported_syntax()) { 114 if (checker.has_supported_syntax()) {
115 // Does not yet generate code. 115 // Does not yet generate code.
116 FastCodeGenerator::MakeCode(literal, script, is_eval, info); 116 FastCodeGenerator::MakeCode(literal, script, is_eval, info);
117 } 117 }
118 } 118 }
119 119
120 return CodeGenerator::MakeCode(literal, script, is_eval, info); 120 return CodeGenerator::MakeCode(literal, script, is_eval, info);
121 } 121 }
122 122
123 123
124 static bool IsValidJSON(FunctionLiteral* lit) {
125 if (lit->body()->length() != 1)
126 return false;
127 Statement* stmt = lit->body()->at(0);
128 if (stmt->AsExpressionStatement() == NULL)
129 return false;
130 Expression* expr = stmt->AsExpressionStatement()->expression();
131 return expr->IsValidJSON();
132 }
133
134
135 static Handle<JSFunction> MakeFunction(bool is_global, 124 static Handle<JSFunction> MakeFunction(bool is_global,
136 bool is_eval, 125 bool is_eval,
137 Compiler::ValidationState validate, 126 Compiler::ValidationState validate,
138 Handle<Script> script, 127 Handle<Script> script,
139 Handle<Context> context, 128 Handle<Context> context,
140 v8::Extension* extension, 129 v8::Extension* extension,
141 ScriptDataImpl* pre_data) { 130 ScriptDataImpl* pre_data) {
142 CompilationZoneScope zone_scope(DELETE_ON_EXIT); 131 CompilationZoneScope zone_scope(DELETE_ON_EXIT);
143 132
144 PostponeInterruptsScope postpone; 133 PostponeInterruptsScope postpone;
145 134
146 ASSERT(!i::Top::global_context().is_null()); 135 ASSERT(!i::Top::global_context().is_null());
147 script->set_context_data((*i::Top::global_context())->data()); 136 script->set_context_data((*i::Top::global_context())->data());
148 137
138 bool is_json = (validate == Compiler::VALIDATE_JSON);
149 #ifdef ENABLE_DEBUGGER_SUPPORT 139 #ifdef ENABLE_DEBUGGER_SUPPORT
150 bool is_json = (validate == Compiler::VALIDATE_JSON);
151 if (is_eval || is_json) { 140 if (is_eval || is_json) {
152 script->set_compilation_type( 141 script->set_compilation_type(
153 is_json ? Smi::FromInt(Script::COMPILATION_TYPE_JSON) : 142 is_json ? Smi::FromInt(Script::COMPILATION_TYPE_JSON) :
154 Smi::FromInt(Script::COMPILATION_TYPE_EVAL)); 143 Smi::FromInt(Script::COMPILATION_TYPE_EVAL));
155 // For eval scripts add information on the function from which eval was 144 // For eval scripts add information on the function from which eval was
156 // called. 145 // called.
157 if (is_eval) { 146 if (is_eval) {
158 JavaScriptFrameIterator it; 147 JavaScriptFrameIterator it;
159 script->set_eval_from_shared( 148 script->set_eval_from_shared(
160 JSFunction::cast(it.frame()->function())->shared()); 149 JSFunction::cast(it.frame()->function())->shared());
161 int offset = static_cast<int>( 150 int offset = static_cast<int>(
162 it.frame()->pc() - it.frame()->code()->instruction_start()); 151 it.frame()->pc() - it.frame()->code()->instruction_start());
163 script->set_eval_from_instructions_offset(Smi::FromInt(offset)); 152 script->set_eval_from_instructions_offset(Smi::FromInt(offset));
164 } 153 }
165 } 154 }
166 155
167 // Notify debugger 156 // Notify debugger
168 Debugger::OnBeforeCompile(script); 157 Debugger::OnBeforeCompile(script);
169 #endif 158 #endif
170 159
171 // Only allow non-global compiles for eval. 160 // Only allow non-global compiles for eval.
172 ASSERT(is_eval || is_global); 161 ASSERT(is_eval || is_global);
173 162
174 // Build AST. 163 // Build AST.
175 FunctionLiteral* lit = MakeAST(is_global, script, extension, pre_data); 164 FunctionLiteral* lit =
165 MakeAST(is_global, script, extension, pre_data, is_json);
176 166
177 // Check for parse errors. 167 // Check for parse errors.
178 if (lit == NULL) { 168 if (lit == NULL) {
179 ASSERT(Top::has_pending_exception()); 169 ASSERT(Top::has_pending_exception());
180 return Handle<JSFunction>::null(); 170 return Handle<JSFunction>::null();
181 } 171 }
182 172
183 // When parsing JSON we do an ordinary parse and then afterwards
184 // check the AST to ensure it was well-formed. If not we give a
185 // syntax error.
186 if (validate == Compiler::VALIDATE_JSON && !IsValidJSON(lit)) {
187 HandleScope scope;
188 Handle<JSArray> args = Factory::NewJSArray(1);
189 Handle<Object> source(script->source());
190 SetElement(args, 0, source);
191 Handle<Object> result = Factory::NewSyntaxError("invalid_json", args);
192 Top::Throw(*result, NULL);
193 return Handle<JSFunction>::null();
194 }
195
196 // Measure how long it takes to do the compilation; only take the 173 // Measure how long it takes to do the compilation; only take the
197 // rest of the function into account to avoid overlap with the 174 // rest of the function into account to avoid overlap with the
198 // parsing statistics. 175 // parsing statistics.
199 HistogramTimer* rate = is_eval 176 HistogramTimer* rate = is_eval
200 ? &Counters::compile_eval 177 ? &Counters::compile_eval
201 : &Counters::compile; 178 : &Counters::compile;
202 HistogramTimerScope timer(rate); 179 HistogramTimerScope timer(rate);
203 180
204 // Compile the code. 181 // Compile the code.
205 CompilationInfo info(Handle<SharedFunctionInfo>::null(), 182 CompilationInfo info(Handle<SharedFunctionInfo>::null(),
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 fun->shared()->set_is_toplevel(is_toplevel); 555 fun->shared()->set_is_toplevel(is_toplevel);
579 fun->shared()->set_inferred_name(*lit->inferred_name()); 556 fun->shared()->set_inferred_name(*lit->inferred_name());
580 fun->shared()->SetThisPropertyAssignmentsInfo( 557 fun->shared()->SetThisPropertyAssignmentsInfo(
581 lit->has_only_simple_this_property_assignments(), 558 lit->has_only_simple_this_property_assignments(),
582 *lit->this_property_assignments()); 559 *lit->this_property_assignments());
583 fun->shared()->set_try_full_codegen(lit->try_full_codegen()); 560 fun->shared()->set_try_full_codegen(lit->try_full_codegen());
584 } 561 }
585 562
586 563
587 } } // namespace v8::internal 564 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/ast.cc ('k') | src/json-delay.js » ('j') | src/parser.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698