@@ -102,7 +102,7 @@ static Handle<Code> MakeCode(FunctionLiteral* literal,
102102
103103
104104static bool IsValidJSON (FunctionLiteral* lit) {
105- if (! lit->body ()->length () = = 1 )
105+ if (lit->body ()->length () ! = 1 )
106106 return false ;
107107 Statement* stmt = lit->body ()->at (0 );
108108 if (stmt->AsExpressionStatement () == NULL )
@@ -114,7 +114,7 @@ static bool IsValidJSON(FunctionLiteral* lit) {
114114
115115static Handle<JSFunction> MakeFunction (bool is_global,
116116 bool is_eval,
117- bool is_json ,
117+ Compiler::ValidationState validate ,
118118 Handle<Script> script,
119119 Handle<Context> context,
120120 v8::Extension* extension,
@@ -129,6 +129,7 @@ static Handle<JSFunction> MakeFunction(bool is_global,
129129 script->set_context_data ((*i::Top::global_context ())->data ());
130130
131131#ifdef ENABLE_DEBUGGER_SUPPORT
132+ bool is_json = (validate == Compiler::VALIDATE_JSON);
132133 if (is_eval || is_json) {
133134 script->set_compilation_type (
134135 is_json ? Smi::FromInt (Script::COMPILATION_TYPE_JSON) :
@@ -162,7 +163,7 @@ static Handle<JSFunction> MakeFunction(bool is_global,
162163 // When parsing JSON we do an ordinary parse and then afterwards
163164 // check the AST to ensure it was well-formed. If not we give a
164165 // syntax error.
165- if (is_json && !IsValidJSON (lit)) {
166+ if (validate == Compiler::VALIDATE_JSON && !IsValidJSON (lit)) {
166167 HandleScope scope;
167168 Handle<JSArray> args = Factory::NewJSArray (1 );
168169 Handle<Object> source (script->source ());
@@ -282,7 +283,7 @@ Handle<JSFunction> Compiler::Compile(Handle<String> source,
282283 // Compile the function and add it to the cache.
283284 result = MakeFunction (true ,
284285 false ,
285- false ,
286+ DONT_VALIDATE_JSON ,
286287 script,
287288 Handle<Context>::null (),
288289 extension,
@@ -305,7 +306,11 @@ Handle<JSFunction> Compiler::Compile(Handle<String> source,
305306Handle<JSFunction> Compiler::CompileEval (Handle<String> source,
306307 Handle<Context> context,
307308 bool is_global,
308- bool is_json) {
309+ ValidationState validate) {
310+ // Note that if validation is required then no path through this
311+ // function is allowed to return a value without validating that
312+ // the input is legal json.
313+
309314 int source_length = source->length ();
310315 Counters::total_eval_size.Increment (source_length);
311316 Counters::total_compile_size.Increment (source_length);
@@ -314,20 +319,26 @@ Handle<JSFunction> Compiler::CompileEval(Handle<String> source,
314319 VMState state (COMPILER);
315320
316321 // Do a lookup in the compilation cache; if the entry is not there,
317- // invoke the compiler and add the result to the cache.
318- Handle<JSFunction> result =
319- CompilationCache::LookupEval (source, context, is_global);
322+ // invoke the compiler and add the result to the cache. If we're
323+ // evaluating json we bypass the cache since we can't be sure a
324+ // potential value in the cache has been validated.
325+ Handle<JSFunction> result;
326+ if (validate == DONT_VALIDATE_JSON)
327+ result = CompilationCache::LookupEval (source, context, is_global);
328+
320329 if (result.is_null ()) {
321330 // Create a script object describing the script to be compiled.
322331 Handle<Script> script = Factory::NewScript (source);
323332 result = MakeFunction (is_global,
324333 true ,
325- is_json ,
334+ validate ,
326335 script,
327336 context,
328337 NULL ,
329338 NULL );
330- if (!result.is_null ()) {
339+ if (!result.is_null () && validate != VALIDATE_JSON) {
340+ // For json it's unlikely that we'll ever see exactly the same
341+ // string again so we don't use the compilation cache.
331342 CompilationCache::PutEval (source, context, is_global, result);
332343 }
333344 }
0 commit comments