www.fgks.org   »   [go: up one dir, main page]

Skip to content

Commit

Permalink
v8binding: Support TypedArrays in [NoAllocDirectCall] methods
Browse files Browse the repository at this point in the history
Data is passed as NADCTypedArray<T> which should be used for both the
NoAllocDirectCall and the regular callbacks to avoid divergent
implementation between NADC and non-NADC code.
NADCTypedArray<T> can be implicitly constructed from the other typed
array view types.

Bug: 1052746
Change-Id: I506d9a164cf339f6b2b99eba5b0ddf9220f567c4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3111816
Commit-Queue: Austin Eng <enga@chromium.org>
Reviewed-by: Yuki Shiino <yukishiino@chromium.org>
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/main@{#940861}
  • Loading branch information
austinEng authored and Chromium LUCI CQ committed Nov 11, 2021
1 parent c632a79 commit b9ca48c
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
37 changes: 37 additions & 0 deletions third_party/blink/renderer/bindings/scripts/bind_gen/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2357,6 +2357,43 @@ def create_definition(symbol_node):
return ("v8::Local<v8::Array>",
S(blink_arg_name,
definition_constructor=create_definition))
elif argument.idl_type.unwrap().is_typed_array_type:
assert "AllowShared" in argument.idl_type.effective_annotations
unwrapped_idl_type = argument.idl_type.unwrap()
element_type_map = {
'Int8Array': 'int8_t',
'Int16Array': 'int16_t',
'Int32Array': 'int32_t',
'BigInt64Array': 'int64_t',
'Uint8Array': 'uint8_t',
'Uint16Array': 'uint16_t',
'Uint32Array': 'uint32_t',
'BigUint64Array': 'uint64_t',
'Uint8ClampedArray': 'uint8_t',
'Float32Array': 'float',
'Float64Array': 'double',
}
element_type = element_type_map.get(
unwrapped_idl_type.keyword_typename)

def create_definition(symbol_node):
binds = {
"v8_arg_name": v8_arg_name,
"blink_arg_name": blink_arg_name,
}

symbol_def_node = SymbolDefinitionNode(
symbol_node,
[F("auto& {blink_arg_name} = {v8_arg_name};", **binds)])
symbol_def_node.accumulate(
CodeGenAccumulator.require_include_headers([
"third_party/blink/renderer/core/typed_arrays/nadc_typed_array_view.h",
]))
return symbol_def_node

return ("const v8::FastApiTypedArray<{}>&".format(element_type),
S(blink_arg_name,
definition_constructor=create_definition))
else:
return (blink_type_info(argument.idl_type).value_t,
S(blink_arg_name,
Expand Down
1 change: 1 addition & 0 deletions third_party/blink/renderer/core/typed_arrays/build.gni
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ blink_core_sources_typed_arrays = [
"dom_typed_array.cc",
"dom_typed_array.h",
"flexible_array_buffer_view.h",
"nadc_typed_array_view.h",
"typed_flexible_array_buffer_view.h",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2021 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.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_TYPED_ARRAYS_NADC_TYPED_ARRAY_VIEW_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_TYPED_ARRAYS_NADC_TYPED_ARRAY_VIEW_H_

#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/typed_arrays/array_buffer_view_helpers.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_typed_array.h"
#include "third_party/blink/renderer/core/typed_arrays/typed_flexible_array_buffer_view.h"
#include "v8/include/v8-fast-api-calls.h"

namespace blink {

// This class is for passing around un-owned bytes as a typed pointer + length
// into functions tagged with extended attribute [NoAllocDirectCall].
// It supports implicit construction from several other MaybeShared typed array
// data types.
//
// IMPORTANT: The data contained by NADCTypedArrayView is NOT OWNED, so caution
// must be taken to ensure it is kept alive.
template <typename T>
class CORE_EXPORT NADCTypedArrayView {
STACK_ALLOCATED();

public:
NADCTypedArrayView(T* data, size_t size) : data_(data), size_(size) {}

NADCTypedArrayView(const NADCTypedArrayView<T>& rhs)
: data_(rhs.Data()), size_(rhs.Size()) {}

// NOLINTNEXTLINE(google-explicit-constructor)
template <typename U>
NADCTypedArrayView(const v8::FastApiTypedArray<U>& rhs)
: size_(rhs.length()) {
U* data = nullptr;
bool is_aligned = rhs.getStorageIfAligned(&data);
DCHECK(is_aligned);
data_ = data;
}

// NOLINTNEXTLINE(google-explicit-constructor)
template <typename U, bool clamped>
NADCTypedArrayView(const TypedFlexibleArrayBufferView<U, clamped>& rhs)
: data_(rhs.DataMaybeOnStack()), size_(rhs.length()) {}

// NOLINTNEXTLINE(google-explicit-constructor)
template <typename U, typename V8TypedArray, bool clamped>
NADCTypedArrayView(
const MaybeShared<DOMTypedArray<U, V8TypedArray, clamped>>& rhs)
: data_(rhs.Get() ? rhs->DataMaybeShared() : nullptr),
size_(rhs.Get() ? rhs->length() : 0) {}

T* Data() const { return data_; }

size_t Size() const { return size_; }

bool IsEmpty() const { return size_ == 0; }

bool IsNull() const { return data_ == nullptr; }

private:
T* data_;
size_t size_;
};

} // namespace blink

#endif // THIRD_PARTY_BLINK_RENDERER_CORE_TYPED_ARRAYS_NADC_TYPED_ARRAY_VIEW_H_

0 comments on commit b9ca48c

Please sign in to comment.