Skip to content

Commit

Permalink
Merge pull request #12 from sunlin-link/webml2
Browse files Browse the repository at this point in the history
Break function execution when DOM exception is thrown
  • Loading branch information
huningxin committed Aug 14, 2018
2 parents 22ef375 + dc3da1f commit 9a86dfe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions third_party/blink/renderer/modules/ml/compilation.cpp
Expand Up @@ -26,6 +26,7 @@ void Compilation::setPreference(int32_t preference, ExceptionState& exception_st
if (is_finished_) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Compilation is finished.");
return;
}
preference_ = preference;
}
Expand Down
8 changes: 6 additions & 2 deletions third_party/blink/renderer/modules/ml/execution.cpp
Expand Up @@ -71,31 +71,35 @@ Execution::~Execution() {
void Execution::setInput(uint32_t index,
MaybeShared<DOMArrayBufferView> data,
ExceptionState& exception_state) {
if (index > inputs_.size()) {
if (index >= inputs_.size()) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Invalid index");
return;
}
std::unique_ptr<OperandInfo>& info = inputs_.at(index);
uint32_t length = data.View()->byteLength();
if (info->length != length) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Invalid data");
return;
}
memcpy(static_cast<void*>(info->mapping.get()), data.View()->BaseAddress(), length);
}

void Execution::setOutput(uint32_t index,
MaybeShared<DOMArrayBufferView> data,
ExceptionState& exception_state) {
if (index > output_buffer_views_.size()) {
if (index >= output_buffer_views_.size()) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Invalid index");
return;
}
std::unique_ptr<OperandInfo>& info = outputs_.at(index);
uint32_t length = data.View()->byteLength();
if (info->length != length) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Invalid data");
return;
}

output_buffer_views_[index] = data.View();
Expand Down
16 changes: 15 additions & 1 deletion third_party/blink/renderer/modules/ml/model.cpp
Expand Up @@ -27,10 +27,12 @@ void Model::addOperand(const OperandOptions& options, ExceptionState& exception_
if (is_finished_) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Model is finished.");
return;
}
if (!options.hasType()) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Operand type is missing.");
return;
}
int32_t type = options.type();

Expand Down Expand Up @@ -60,11 +62,13 @@ void Model::setOperandValue(uint32_t index,
if (is_finished_) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Model is finished.");
return;
}

if (index > model_info_->operands.size()) {
if (index >= model_info_->operands.size()) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Index is invalid.");
return;
}

const ml::mojom::blink::OperandPtr& operand =
Expand All @@ -76,25 +80,29 @@ void Model::setOperandValue(uint32_t index,
operand->type == NeuralNetworkContext::kTensorFloat32)) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Data type is invalid.");
return;
}

if (view_type == WTF::ArrayBufferView::kTypeInt32 &&
!(operand->type == NeuralNetworkContext::kInt32 ||
operand->type == NeuralNetworkContext::kTensorInt32)) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Data type is invalid.");
return;
}

if (view_type == WTF::ArrayBufferView::kTypeUint32 &&
(operand->type != NeuralNetworkContext::kUint32)) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Data type is invalid.");
return;
}

if (view_type == WTF::ArrayBufferView::kTypeUint8 &&
(operand->type != NeuralNetworkContext::kTensorQuant8Asymm)) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Data type is invalid.");
return;
}

model_info_->values.push_back(ml::mojom::blink::OperandValueInfo::New(index, 0, 0));
Expand All @@ -108,17 +116,20 @@ void Model::addOperation(int32_t type,
if (is_finished_) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Model is finished.");
return;
}
for (size_t i = 0; i < inputs.size(); ++i) {
if (inputs[i] > model_info_->operands.size()) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Inputs is invalid.");
return;
}
}
for (size_t i = 0; i < outputs.size(); ++i) {
if (outputs[i] > model_info_->operands.size()) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Outputs is invalid.");
return;
}
}
model_info_->operations.push_back(
Expand All @@ -131,17 +142,20 @@ void Model::identifyInputsAndOutputs(Vector<uint32_t>& inputs,
if (is_finished_) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Model is finished.");
return;
}
for (size_t i = 0; i < inputs.size(); ++i) {
if (inputs[i] > model_info_->operands.size()) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Inputs is invalid.");
return;
}
}
for (size_t i = 0; i < outputs.size(); ++i) {
if (outputs[i] > model_info_->operands.size()) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Outputs is invalid.");
return;
}
}
model_info_->inputs = inputs;
Expand Down

0 comments on commit 9a86dfe

Please sign in to comment.