OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/compiler.h" | 5 #include "src/compiler.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "src/asmjs/asm-js.h" | 10 #include "src/asmjs/asm-js.h" |
(...skipping 1247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1258 return BASELINE; | 1258 return BASELINE; |
1259 } | 1259 } |
1260 } else { | 1260 } else { |
1261 return OPTIMIZED; | 1261 return OPTIMIZED; |
1262 } | 1262 } |
1263 } | 1263 } |
1264 | 1264 |
1265 MaybeHandle<JSFunction> Compiler::GetFunctionFromEval( | 1265 MaybeHandle<JSFunction> Compiler::GetFunctionFromEval( |
1266 Handle<String> source, Handle<SharedFunctionInfo> outer_info, | 1266 Handle<String> source, Handle<SharedFunctionInfo> outer_info, |
1267 Handle<Context> context, LanguageMode language_mode, | 1267 Handle<Context> context, LanguageMode language_mode, |
1268 ParseRestriction restriction, int eval_scope_position, int eval_position, | 1268 ParseRestriction restriction, int parameters_end_pos, |
1269 int line_offset, int column_offset, Handle<Object> script_name, | 1269 int eval_scope_position, int eval_position, int line_offset, |
1270 int column_offset, Handle<Object> script_name, | |
1270 ScriptOriginOptions options) { | 1271 ScriptOriginOptions options) { |
1271 Isolate* isolate = source->GetIsolate(); | 1272 Isolate* isolate = source->GetIsolate(); |
1272 int source_length = source->length(); | 1273 int source_length = source->length(); |
1273 isolate->counters()->total_eval_size()->Increment(source_length); | 1274 isolate->counters()->total_eval_size()->Increment(source_length); |
1274 isolate->counters()->total_compile_size()->Increment(source_length); | 1275 isolate->counters()->total_compile_size()->Increment(source_length); |
1275 | 1276 |
1277 // The cache lookup key needs to be aware of the separation between the | |
1278 // parameters and the body to prevent this valid invocation: | |
1279 // Function("", "function anonymous(\n/**/) {\n}"); | |
1280 // from adding an entry that falsely approves this invalid invocation: | |
1281 // Function("\n/**/) {\nfunction anonymous(", "}"); | |
1282 // The actual eval_scope_position for indirect eval and CreateDynamicFunction | |
1283 // is unused (just 0), which means it's an available field to use to indicate | |
1284 // this separation. But to make sure we're not causing other false hits, we | |
1285 // negate the scope position. | |
1286 if (FLAG_harmony_function_tostring && | |
1287 restriction == ONLY_SINGLE_FUNCTION_LITERAL && | |
1288 parameters_end_pos != kNoSourcePosition) { | |
1289 // use the parameters_end_pos as the eval_scope_position in the eval cache. | |
1290 DCHECK_EQ(eval_scope_position, 0); | |
1291 eval_scope_position = -parameters_end_pos; | |
Dan Ehrenberg
2016/12/06 00:32:13
This seems like a valid implementation strategy, t
jwolfe
2017/01/13 00:28:47
I added documentation to the StringSharedKey const
| |
1292 } | |
1276 CompilationCache* compilation_cache = isolate->compilation_cache(); | 1293 CompilationCache* compilation_cache = isolate->compilation_cache(); |
1277 MaybeHandle<SharedFunctionInfo> maybe_shared_info = | 1294 MaybeHandle<SharedFunctionInfo> maybe_shared_info = |
1278 compilation_cache->LookupEval(source, outer_info, context, language_mode, | 1295 compilation_cache->LookupEval(source, outer_info, context, language_mode, |
1279 eval_scope_position); | 1296 eval_scope_position); |
1280 Handle<SharedFunctionInfo> shared_info; | 1297 Handle<SharedFunctionInfo> shared_info; |
1281 | 1298 |
1282 Handle<Script> script; | 1299 Handle<Script> script; |
1283 if (!maybe_shared_info.ToHandle(&shared_info)) { | 1300 if (!maybe_shared_info.ToHandle(&shared_info)) { |
1284 script = isolate->factory()->NewScript(source); | 1301 script = isolate->factory()->NewScript(source); |
1285 if (FLAG_trace_deopt) Script::InitLineEnds(script); | 1302 if (FLAG_trace_deopt) Script::InitLineEnds(script); |
1286 if (!script_name.is_null()) { | 1303 if (!script_name.is_null()) { |
1287 script->set_name(*script_name); | 1304 script->set_name(*script_name); |
1288 script->set_line_offset(line_offset); | 1305 script->set_line_offset(line_offset); |
1289 script->set_column_offset(column_offset); | 1306 script->set_column_offset(column_offset); |
1290 } | 1307 } |
1291 script->set_origin_options(options); | 1308 script->set_origin_options(options); |
1292 script->set_compilation_type(Script::COMPILATION_TYPE_EVAL); | 1309 script->set_compilation_type(Script::COMPILATION_TYPE_EVAL); |
1293 Script::SetEvalOrigin(script, outer_info, eval_position); | 1310 Script::SetEvalOrigin(script, outer_info, eval_position); |
1294 | 1311 |
1295 Zone zone(isolate->allocator(), ZONE_NAME); | 1312 Zone zone(isolate->allocator(), ZONE_NAME); |
1296 ParseInfo parse_info(&zone, script); | 1313 ParseInfo parse_info(&zone, script); |
1297 CompilationInfo info(&parse_info, Handle<JSFunction>::null()); | 1314 CompilationInfo info(&parse_info, Handle<JSFunction>::null()); |
1298 parse_info.set_eval(); | 1315 parse_info.set_eval(); |
1299 parse_info.set_language_mode(language_mode); | 1316 parse_info.set_language_mode(language_mode); |
1300 parse_info.set_parse_restriction(restriction); | 1317 parse_info.set_parse_restriction(restriction); |
1318 parse_info.set_parameters_end_pos(parameters_end_pos); | |
1301 if (!context->IsNativeContext()) { | 1319 if (!context->IsNativeContext()) { |
1302 parse_info.set_outer_scope_info(handle(context->scope_info())); | 1320 parse_info.set_outer_scope_info(handle(context->scope_info())); |
1303 } | 1321 } |
1304 | 1322 |
1305 shared_info = CompileToplevel(&info); | 1323 shared_info = CompileToplevel(&info); |
1306 | 1324 |
1307 if (shared_info.is_null()) { | 1325 if (shared_info.is_null()) { |
1308 return MaybeHandle<JSFunction>(); | 1326 return MaybeHandle<JSFunction>(); |
1309 } else { | 1327 } else { |
1310 // If caller is strict mode, the result must be in strict mode as well. | 1328 // If caller is strict mode, the result must be in strict mode as well. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1342 // Callback set. Let it decide if code generation is allowed. | 1360 // Callback set. Let it decide if code generation is allowed. |
1343 VMState<EXTERNAL> state(isolate); | 1361 VMState<EXTERNAL> state(isolate); |
1344 return callback(v8::Utils::ToLocal(context)); | 1362 return callback(v8::Utils::ToLocal(context)); |
1345 } | 1363 } |
1346 } | 1364 } |
1347 | 1365 |
1348 } // namespace | 1366 } // namespace |
1349 | 1367 |
1350 MaybeHandle<JSFunction> Compiler::GetFunctionFromString( | 1368 MaybeHandle<JSFunction> Compiler::GetFunctionFromString( |
1351 Handle<Context> context, Handle<String> source, | 1369 Handle<Context> context, Handle<String> source, |
1352 ParseRestriction restriction) { | 1370 ParseRestriction restriction, int parameters_end_pos) { |
1353 Isolate* const isolate = context->GetIsolate(); | 1371 Isolate* const isolate = context->GetIsolate(); |
1354 Handle<Context> native_context(context->native_context(), isolate); | 1372 Handle<Context> native_context(context->native_context(), isolate); |
1355 | 1373 |
1356 // Check if native context allows code generation from | 1374 // Check if native context allows code generation from |
1357 // strings. Throw an exception if it doesn't. | 1375 // strings. Throw an exception if it doesn't. |
1358 if (native_context->allow_code_gen_from_strings()->IsFalse(isolate) && | 1376 if (native_context->allow_code_gen_from_strings()->IsFalse(isolate) && |
1359 !CodeGenerationFromStringsAllowed(isolate, native_context)) { | 1377 !CodeGenerationFromStringsAllowed(isolate, native_context)) { |
1360 Handle<Object> error_message = | 1378 Handle<Object> error_message = |
1361 native_context->ErrorMessageForCodeGenerationFromStrings(); | 1379 native_context->ErrorMessageForCodeGenerationFromStrings(); |
1362 THROW_NEW_ERROR(isolate, NewEvalError(MessageTemplate::kCodeGenFromStrings, | 1380 THROW_NEW_ERROR(isolate, NewEvalError(MessageTemplate::kCodeGenFromStrings, |
1363 error_message), | 1381 error_message), |
1364 JSFunction); | 1382 JSFunction); |
1365 } | 1383 } |
1366 | 1384 |
1367 // Compile source string in the native context. | 1385 // Compile source string in the native context. |
1368 int eval_scope_position = 0; | 1386 int eval_scope_position = 0; |
1369 int eval_position = kNoSourcePosition; | 1387 int eval_position = kNoSourcePosition; |
1370 Handle<SharedFunctionInfo> outer_info(native_context->closure()->shared()); | 1388 Handle<SharedFunctionInfo> outer_info(native_context->closure()->shared()); |
1371 return Compiler::GetFunctionFromEval(source, outer_info, native_context, | 1389 return Compiler::GetFunctionFromEval(source, outer_info, native_context, |
1372 SLOPPY, restriction, eval_scope_position, | 1390 SLOPPY, restriction, parameters_end_pos, |
1373 eval_position); | 1391 eval_scope_position, eval_position); |
1374 } | 1392 } |
1375 | 1393 |
1376 Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfoForScript( | 1394 Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfoForScript( |
1377 Handle<String> source, Handle<Object> script_name, int line_offset, | 1395 Handle<String> source, Handle<Object> script_name, int line_offset, |
1378 int column_offset, ScriptOriginOptions resource_options, | 1396 int column_offset, ScriptOriginOptions resource_options, |
1379 Handle<Object> source_map_url, Handle<Context> context, | 1397 Handle<Object> source_map_url, Handle<Context> context, |
1380 v8::Extension* extension, ScriptData** cached_data, | 1398 v8::Extension* extension, ScriptData** cached_data, |
1381 ScriptCompiler::CompileOptions compile_options, NativesFlag natives, | 1399 ScriptCompiler::CompileOptions compile_options, NativesFlag natives, |
1382 bool is_module) { | 1400 bool is_module) { |
1383 Isolate* isolate = source->GetIsolate(); | 1401 Isolate* isolate = source->GetIsolate(); |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1723 DCHECK(shared->is_compiled()); | 1741 DCHECK(shared->is_compiled()); |
1724 function->set_literals(cached.literals); | 1742 function->set_literals(cached.literals); |
1725 } else if (shared->is_compiled()) { | 1743 } else if (shared->is_compiled()) { |
1726 // TODO(mvstanton): pass pretenure flag to EnsureLiterals. | 1744 // TODO(mvstanton): pass pretenure flag to EnsureLiterals. |
1727 JSFunction::EnsureLiterals(function); | 1745 JSFunction::EnsureLiterals(function); |
1728 } | 1746 } |
1729 } | 1747 } |
1730 | 1748 |
1731 } // namespace internal | 1749 } // namespace internal |
1732 } // namespace v8 | 1750 } // namespace v8 |
OLD | NEW |