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

Side by Side Diff: src/runtime.cc

Issue 13704010: Generator objects can suspend (Closed) Base URL: git://github.com/v8/v8.git@master
Patch Set: Use sentinel values for continuations Created 7 years, 8 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/runtime.h ('k') | src/x64/full-codegen-x64.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 2277 matching lines...) Expand 10 before | Expand all | Expand 10 after
2288 CONVERT_SMI_ARG_CHECKED(num, 1); 2288 CONVERT_SMI_ARG_CHECKED(num, 1);
2289 RUNTIME_ASSERT(num >= 0); 2289 RUNTIME_ASSERT(num >= 0);
2290 SetExpectedNofProperties(function, num); 2290 SetExpectedNofProperties(function, num);
2291 return isolate->heap()->undefined_value(); 2291 return isolate->heap()->undefined_value();
2292 } 2292 }
2293 2293
2294 2294
2295 RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateJSGeneratorObject) { 2295 RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateJSGeneratorObject) {
2296 NoHandleAllocation ha(isolate); 2296 NoHandleAllocation ha(isolate);
2297 ASSERT(args.length() == 0); 2297 ASSERT(args.length() == 0);
2298
2298 JavaScriptFrameIterator it(isolate); 2299 JavaScriptFrameIterator it(isolate);
2299 JavaScriptFrame* frame = it.frame(); 2300 JavaScriptFrame* frame = it.frame();
2300 JSFunction* function = JSFunction::cast(frame->function()); 2301 JSFunction* function = JSFunction::cast(frame->function());
2301 RUNTIME_ASSERT(function->shared()->is_generator()); 2302 RUNTIME_ASSERT(function->shared()->is_generator());
2302 2303
2303 JSGeneratorObject* generator; 2304 JSGeneratorObject* generator;
2304 if (frame->IsConstructor()) { 2305 if (frame->IsConstructor()) {
2305 generator = JSGeneratorObject::cast(frame->receiver()); 2306 generator = JSGeneratorObject::cast(frame->receiver());
2306 } else { 2307 } else {
2307 MaybeObject* maybe_generator = 2308 MaybeObject* maybe_generator =
2308 isolate->heap()->AllocateJSGeneratorObject(function); 2309 isolate->heap()->AllocateJSGeneratorObject(function);
2309 if (!maybe_generator->To(&generator)) return maybe_generator; 2310 if (!maybe_generator->To(&generator)) return maybe_generator;
2310 } 2311 }
2311 generator->set_function(function); 2312 generator->set_function(function);
2312 generator->set_context(isolate->heap()->undefined_value()); 2313 generator->set_context(Context::cast(frame->context()));
2313 generator->set_continuation(0); 2314 generator->set_continuation(0);
2314 generator->set_operand_stack(isolate->heap()->empty_fixed_array()); 2315 generator->set_operand_stack(isolate->heap()->empty_fixed_array());
2315 2316
2316 return generator; 2317 return generator;
2317 } 2318 }
2318 2319
2319 2320
2321 RUNTIME_FUNCTION(MaybeObject*, Runtime_SuspendJSGeneratorObject) {
2322 HandleScope scope(isolate);
2323 ASSERT(args.length() == 1);
2324 CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator_object, 0);
2325
2326 JavaScriptFrameIterator stack_iterator(isolate);
2327 JavaScriptFrame *frame = stack_iterator.frame();
2328 Handle<JSFunction> function(JSFunction::cast(frame->function()));
2329 RUNTIME_ASSERT(function->shared()->is_generator());
2330
2331 intptr_t offset = frame->pc() - function->code()->instruction_start();
2332 ASSERT(*function == generator_object->function());
2333 ASSERT(offset > 0 && Smi::IsValid(offset));
2334 generator_object->set_continuation(offset);
2335
2336 // Generator functions force context allocation for locals, so Local0 points
2337 // to the bottom of the operand stack. Assume the stack grows down.
2338 //
2339 // TODO(wingo): Move these magical calculations to frames.h when the
2340 // generators implementation has stabilized.
2341 intptr_t stack_size_in_bytes =
2342 (frame->fp() + JavaScriptFrameConstants::kLocal0Offset) -
2343 (frame->sp() - kPointerSize);
2344 ASSERT(IsAddressAligned(frame->fp(), kPointerSize));
2345 ASSERT(IsAligned(stack_size_in_bytes, kPointerSize));
2346 ASSERT(stack_size_in_bytes >= 0);
2347 ASSERT(Smi::IsValid(stack_size_in_bytes));
2348 unsigned stack_size = stack_size_in_bytes >> kPointerSizeLog2;
2349
2350 // We expect there to be at least two values on the stack: the return value of
2351 // the yield expression, and the argument to this runtime call. Neither of
2352 // those should be saved.
2353 ASSERT(stack_size >= 2);
2354 stack_size -= 2;
2355
2356 if (stack_size == 0) {
2357 ASSERT_EQ(generator_object->operand_stack(),
2358 isolate->heap()->empty_fixed_array());
2359 // If there are no operands on the stack, there shouldn't be a handler
2360 // active either. Also, the active context will be the same as the function
2361 // itself, so there is no need to save the context.
2362 ASSERT_EQ(frame->context(), generator_object->context());
2363 ASSERT(!frame->HasHandler());
2364 } else {
2365 generator_object->set_context(Context::cast(frame->context()));
2366 // TODO(wingo): Save the operand stack and/or the stack handlers.
2367 UNIMPLEMENTED();
2368 }
2369
2370 // The return value is the hole for a suspend return, and anything else for a
2371 // resume return.
2372 return isolate->heap()->the_hole_value();
2373 }
2374
2375
2320 MUST_USE_RESULT static MaybeObject* CharFromCode(Isolate* isolate, 2376 MUST_USE_RESULT static MaybeObject* CharFromCode(Isolate* isolate,
2321 Object* char_code) { 2377 Object* char_code) {
2322 uint32_t code; 2378 uint32_t code;
2323 if (char_code->ToArrayIndex(&code)) { 2379 if (char_code->ToArrayIndex(&code)) {
2324 if (code <= 0xffff) { 2380 if (code <= 0xffff) {
2325 return isolate->heap()->LookupSingleCharacterStringFromCode(code); 2381 return isolate->heap()->LookupSingleCharacterStringFromCode(code);
2326 } 2382 }
2327 } 2383 }
2328 return isolate->heap()->empty_string(); 2384 return isolate->heap()->empty_string();
2329 } 2385 }
(...skipping 10779 matching lines...) Expand 10 before | Expand all | Expand 10 after
13109 // Handle last resort GC and make sure to allow future allocations 13165 // Handle last resort GC and make sure to allow future allocations
13110 // to grow the heap without causing GCs (if possible). 13166 // to grow the heap without causing GCs (if possible).
13111 isolate->counters()->gc_last_resort_from_js()->Increment(); 13167 isolate->counters()->gc_last_resort_from_js()->Increment();
13112 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, 13168 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags,
13113 "Runtime::PerformGC"); 13169 "Runtime::PerformGC");
13114 } 13170 }
13115 } 13171 }
13116 13172
13117 13173
13118 } } // namespace v8::internal 13174 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/runtime.h ('k') | src/x64/full-codegen-x64.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698