Skip to content

Commit

Permalink
node: Add converter for base::Value
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Jan 26, 2018
1 parent 8bb2b42 commit babf9d0
Show file tree
Hide file tree
Showing 8 changed files with 790 additions and 1 deletion.
1 change: 0 additions & 1 deletion LICENSE.chromium
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

5 changes: 5 additions & 0 deletions node_yue/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ loadable_module("node_yue") {
"binding_gui.cc",
"binding_signal.cc",
"binding_signal.h",
"binding_values.cc",
"binding_values.h",
"chrome_view_mac.mm",
"chrome_view_mac.h",
"delay_load_hook_win.cc",
Expand All @@ -24,6 +26,9 @@ loadable_module("node_yue") {
"node_integration_mac.h",
"node_integration_win.cc",
"node_integration_win.h",
"v8_value_converter.h",
"v8_value_converter_impl.cc",
"v8_value_converter_impl.h",
]

deps = [
Expand Down
1 change: 1 addition & 0 deletions node_yue/binding_gui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "nativeui/nativeui.h"
#include "node_yue/binding_signal.h"
#include "node_yue/binding_values.h"
#include "node_yue/node_integration.h"

#if defined(OS_MACOSX)
Expand Down
35 changes: 35 additions & 0 deletions node_yue/binding_values.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2018 Cheng Zhao. All rights reserved.
// Use of this source code is governed by the license that can be found in the
// LICENSE file.

#include "node_yue/binding_values.h"

#include <memory>
#include <utility>

#include "node_yue/v8_value_converter.h"

namespace vb {

using node_yue::V8ValueConverter;

// static
v8::Local<v8::Value> Type<base::Value>::ToV8(v8::Local<v8::Context> context,
const base::Value& value) {
std::unique_ptr<V8ValueConverter> converter = V8ValueConverter::Create();
return converter->ToV8Value(&value, context);
}

// statc
bool Type<base::Value>::FromV8(v8::Local<v8::Context> context,
v8::Local<v8::Value> value,
base::Value* out) {
std::unique_ptr<V8ValueConverter> converter = V8ValueConverter::Create();
std::unique_ptr<base::Value> result = converter->FromV8Value(value, context);
if (!result)
return false;
*out = std::move(*result.release());
return true;
}

} // namespace vb
25 changes: 25 additions & 0 deletions node_yue/binding_values.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2018 Cheng Zhao. All rights reserved.
// Use of this source code is governed by the license that can be found in the
// LICENSE file.

#ifndef NODE_YUE_BINDING_VALUES_H_
#define NODE_YUE_BINDING_VALUES_H_

#include "base/values.h"
#include "v8binding/v8binding.h"

namespace vb {

template<>
struct Type<base::Value> {
static constexpr const char* name = "Value";
static v8::Local<v8::Value> ToV8(v8::Local<v8::Context> context,
const base::Value& value);
static bool FromV8(v8::Local<v8::Context> context,
v8::Local<v8::Value> value,
base::Value* out);
};

} // namespace vb

#endif // NODE_YUE_BINDING_VALUES_H_
86 changes: 86 additions & 0 deletions node_yue/v8_value_converter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2018 Cheng Zhao. All rights reserved.
// Copyright 2012 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.chromium file.

#ifndef NODE_YUE_V8_VALUE_CONVERTER_H_
#define NODE_YUE_V8_VALUE_CONVERTER_H_

#include <memory>

#include "v8binding/v8binding.h"

namespace base {
class Value;
}

namespace node_yue {

// Converts between v8::Value (JavaScript values in the v8 heap) and Chrome's
// values (from base/values.h). Lists and dictionaries are converted
// recursively.
//
// The JSON types (null, boolean, string, number, array, and object) as well as
// binary values are supported. For binary values, we convert to WebKit
// ArrayBuffers, and support converting from an ArrayBuffer or any of the
// ArrayBufferView subclasses (Uint8Array, etc.).
class V8ValueConverter {
public:
static std::unique_ptr<V8ValueConverter> Create();

virtual ~V8ValueConverter() {}

// If true, Date objects are converted into DoubleValues with the number of
// seconds since Unix epoch.
//
// Otherwise they are converted into DictionaryValues with whatever additional
// properties has been set on them.
virtual void SetDateAllowed(bool val) = 0;

// If true, RegExp objects are converted into StringValues with the regular
// expression between / and /, for example "/ab?c/".
//
// Otherwise they are converted into DictionaryValues with whatever additional
// properties has been set on them.
virtual void SetRegExpAllowed(bool val) = 0;

// If true, Function objects are converted into DictionaryValues with whatever
// additional properties has been set on them.
//
// Otherwise they are treated as unsupported, see FromV8Value.
virtual void SetFunctionAllowed(bool val) = 0;

// If true, null values are stripped from objects. This is often useful when
// converting arguments to extension APIs.
virtual void SetStripNullFromObjects(bool val) = 0;

// If true, treats -0 as an integer. Otherwise, -0 is converted to a double.
virtual void SetConvertNegativeZeroToInt(bool val) = 0;

// Converts a base::Value to a v8::Value.
//
// Unsupported types are replaced with null. If an array or object throws
// while setting a value, that property or item is skipped, leaving a hole in
// the case of arrays.
// TODO(dcheng): This should just take a const reference.
virtual v8::Local<v8::Value> ToV8Value(
const base::Value* value,
v8::Local<v8::Context> context) const = 0;

// Converts a v8::Value to base::Value.
//
// Unsupported types (unless explicitly configured) are not converted, so
// this method may return NULL -- the exception is when converting arrays,
// where unsupported types are converted to Value(Type::NONE).
//
// Likewise, if an object throws while converting a property it will not be
// converted, whereas if an array throws while converting an item it will be
// converted to Value(Type::NONE).
virtual std::unique_ptr<base::Value> FromV8Value(
v8::Local<v8::Value> value,
v8::Local<v8::Context> context) const = 0;
};

} // namespace node_yue

#endif // NODE_YUE_V8_VALUE_CONVERTER_H_
Loading

0 comments on commit babf9d0

Please sign in to comment.