Chromium Code Reviews| Index: Source/platform/streams/WebStreams.cpp |
| diff --git a/Source/platform/streams/WebStreams.cpp b/Source/platform/streams/WebStreams.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..29637c3a85dfe00de7edb293ef2edf930ae23b05 |
| --- /dev/null |
| +++ b/Source/platform/streams/WebStreams.cpp |
| @@ -0,0 +1,92 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "config.h" |
| +#include "public/platform/WebStreams.h" |
| + |
| +#include "wtf/Assertions.h" |
| + |
| +namespace blink { |
| + |
| +const char kStreamsExtensionName[] = "blink/streams"; |
| + |
| +// TODO use js2c.py in a gyp file somehow to externalize this into a .js file |
| +// Apparently the presubmit checks hate multiline strings because they hate freedom |
| +const char kStreamsExtensionCode[] = R"JSCODE( |
| +'use strict'; |
| + |
| +native function SET_PRIVATE(); |
| +native function GET_PRIVATE(); |
| + |
| +this.ReadableStream = ReadableStream; |
| + |
| +function ReadableStream(underlyingSource) { |
| + if (underlyingSource === undefined) { |
| + underlyingSource = {}; |
| + } |
| + |
| + SET_PRIVATE(this, 'ReadableStream#underlyingSource', underlyingSource); |
| +} |
| + |
| +ReadableStream.prototype.logUnderlyingSource = function () { |
| + console.log(GET_PRIVATE(this, 'ReadableStream#underlyingSource')); |
| +}; |
| + |
| +)JSCODE"; |
| + |
| +class StreamsExtensionImpl : public v8::Extension { |
| + public: |
| + StreamsExtensionImpl() : v8::Extension(kStreamsExtensionName, kStreamsExtensionCode) { } |
| + |
| + private: |
| + v8::Handle<v8::FunctionTemplate> GetNativeFunctionTemplate( |
| + v8::Isolate* isolate, v8::Handle<v8::String> name) override |
| + { |
| + |
| + if (name->Equals(v8::String::NewFromUtf8(isolate, "SET_PRIVATE"))) { |
| + return v8::FunctionTemplate::New(isolate, SetPrivate); |
| + } |
| + if (name->Equals(v8::String::NewFromUtf8(isolate, "GET_PRIVATE"))) { |
| + return v8::FunctionTemplate::New(isolate, GetPrivate); |
| + } |
| + |
| + return v8::Handle<v8::FunctionTemplate>(); |
| + } |
| + |
| + static void SetPrivate(const v8::FunctionCallbackInfo<v8::Value>& info) |
| + { |
| + ASSERT(info.Length() == 3 && info[0]->IsObject() && info[1]->IsString()); |
| + |
| + auto isolate = info.GetIsolate(); |
| + v8::HandleScope scope(isolate); |
| + auto object = info[0].As<v8::Object>(); |
| + auto name = info[1].As<v8::String>(); |
| + auto value = info[2]; |
| + |
| + auto privateSymbol = v8::Private::ForApi(isolate, name); |
| + object->SetPrivate(privateSymbol, value); |
| + } |
| + |
| + static void GetPrivate(const v8::FunctionCallbackInfo<v8::Value>& info) |
| + { |
| + ASSERT(info.Length() == 2 && info[0]->IsObject() && info[1]->IsString()); |
| + |
| + auto isolate = info.GetIsolate(); |
| + v8::HandleScope scope(isolate); |
| + auto object = info[0].As<v8::Object>(); |
| + auto name = info[1].As<v8::String>(); |
| + |
| + auto privateSymbol = v8::Private::ForApi(isolate, name); |
|
Dmitry Lomov (no reviews)
2015/02/13 16:39:56
Hmm I do not get it. Seems like you are creating a
|
| + auto value = object->GetPrivate(privateSymbol); |
| + |
| + info.GetReturnValue().Set(value); |
| + } |
| +}; |
| + |
| +v8::Extension* WebStreams::CreateV8Extension() |
| +{ |
| + return new StreamsExtensionImpl(); |
| +} |
| + |
| +} // namespace blink |