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

Side by Side Diff: test/cctest/test-api.cc

Issue 366153002: Add script streaming API (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: added tests + fixed compilation flags (!) Created 6 years, 3 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 | Annotate | Revision Log
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 22982 matching lines...) Expand 10 before | Expand all | Expand 10 after
22993 desc = x->GetOwnPropertyDescriptor(v8_str("p1")); 22993 desc = x->GetOwnPropertyDescriptor(v8_str("p1"));
22994 Local<Function> set = 22994 Local<Function> set =
22995 Local<Function>::Cast(Local<Object>::Cast(desc)->Get(v8_str("set"))); 22995 Local<Function>::Cast(Local<Object>::Cast(desc)->Get(v8_str("set")));
22996 Local<Function> get = 22996 Local<Function> get =
22997 Local<Function>::Cast(Local<Object>::Cast(desc)->Get(v8_str("get"))); 22997 Local<Function>::Cast(Local<Object>::Cast(desc)->Get(v8_str("get")));
22998 CHECK_EQ(v8_num(13), get->Call(x, 0, NULL)); 22998 CHECK_EQ(v8_num(13), get->Call(x, 0, NULL));
22999 Handle<Value> args[] = { v8_num(14) }; 22999 Handle<Value> args[] = { v8_num(14) };
23000 set->Call(x, 1, args); 23000 set->Call(x, 1, args);
23001 CHECK_EQ(v8_num(14), get->Call(x, 0, NULL)); 23001 CHECK_EQ(v8_num(14), get->Call(x, 0, NULL));
23002 } 23002 }
23003
23004 class TestSourceStream : public v8::ScriptCompiler::ExternalSourceStream {
23005 public:
23006 explicit TestSourceStream(const char** chunks) : chunks_(chunks), index_(0) {
23007 SetEncoding(v8::ScriptCompiler::ExternalSourceStream::ONE_BYTE);
23008 }
23009
23010 virtual unsigned GetMoreData(const char** src) {
23011 // Unlike in real use cases, this function will never block.
23012 if (chunks_[index_] == NULL) {
23013 return 0;
23014 }
23015 // Copy the data, since the caller takes ownership of it.
23016 unsigned len = strlen(chunks_[index_]);
Sven Panne 2014/09/01 09:07:46 size_t
marja 2014/09/01 11:58:17 Done.
23017 // snprintf will NULL-terminate, so reserve space for the NULL too. However,
Sven Panne 2014/09/01 09:07:46 Nit: It zero-terminates, not NULL-terminates. :-)
marja 2014/09/01 11:58:17 Done.
23018 // the caller is not interested in the NULL; we will return the real length
23019 // below.
23020 char* copy = new char[len + 1];
23021 snprintf(copy, len + 1, "%s", chunks_[index_]);
Sven Panne 2014/09/01 09:07:46 Wouldn't memcpy simplify things here and below?
marja 2014/09/01 11:58:17 Yes, but presubmit whines about it :(
jochen (gone - plz use gerrit) 2014/09/01 13:16:32 why not strncpy?
marja 2014/09/01 13:23:25 Argh, this discussion was confused. So I original
23022 *src = copy;
23023 ++index_;
23024 return len;
23025 }
23026
23027 // Helper for constructing a string from chunks (the compilation needs it
23028 // too).
23029 static char* FullSourceString(const char** chunks) {
23030 size_t total_len = 0;
23031 for (size_t i = 0; chunks[i] != NULL; ++i) {
23032 total_len += strlen(chunks[i]);
23033 }
23034 char* full_string = new char[total_len + 1];
23035 size_t offset = 0;
23036 for (size_t i = 0; chunks[i] != NULL; ++i) {
23037 size_t len = strlen(chunks[i]);
23038 snprintf(full_string + offset, len + 1, "%s", chunks[i]);
23039 offset += len;
23040 }
23041 return full_string;
23042 }
23043
23044 private:
23045 const char** chunks_;
23046 unsigned index_;
23047 };
23048
23049
23050 TEST(StreamingSimpleScript) {
23051 LocalContext env;
23052 v8::Isolate* isolate = env->GetIsolate();
23053 v8::HandleScope scope(isolate);
23054
23055 // This script is unrealistically small, since no one chunk is enough to fill
23056 // the backing buffer of Scanner, let alone overflow it. Other tests will
23057 // cover those cases.
23058 const char* chunks[] = {"function foo() { ret", "urn 3; } f", "oo(); ", NULL};
23059 v8::ScriptCompiler::StreamedSource source(new TestSourceStream(chunks));
23060 v8::ScriptCompiler::ScriptStreamingTask* task =
23061 v8::ScriptCompiler::StartStreamingScript(isolate, &source);
23062
23063 // TestSourceStream::GetMoreData won't block, so it's okay to just run the
23064 // task here in the main thread.
23065 task->Run();
23066 delete task;
23067
23068 v8::ScriptOrigin origin(v8_str("http://foo.com"));
23069 char* full_source = TestSourceStream::FullSourceString(chunks);
23070 v8::Handle<Script> script = v8::ScriptCompiler::Compile(
23071 isolate, &source, v8_str(full_source), origin);
23072 CHECK(!script.IsEmpty());
23073 v8::Handle<Value> result(script->Run());
23074 CHECK_EQ(3, result->Int32Value());
23075 delete[] full_source;
23076 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698