Skip to content

Commit

Permalink
Use RangeError instead + little tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus committed Sep 9, 2020
1 parent 1f8747b commit 22a2bd8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
27 changes: 13 additions & 14 deletions core/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,23 +658,22 @@ fn decode(
)
};

// If `String::new_from_utf8()` fails it most likely means that we're trying to
// create a string that is too big. In that case return TypeError as a result.
// If `String::new_from_utf8()` returns `None`, this means that the
// length of the decoded string would be longer than what V8 can
// handle. In this case we return `RangeError`.
//
// For more details see:
// - https://github.com/denoland/deno/issues/6649
// - https://encoding.spec.whatwg.org/#dom-textdecoder-decode
let text_str =
match v8::String::new_from_utf8(scope, &buf, v8::NewStringType::Normal) {
Some(text) => text,
None => {
let msg = v8::String::new(scope, "Failed to decode").unwrap();
let exception = v8::Exception::type_error(scope, msg);
scope.throw_exception(exception);
return;
}
};
rv.set(text_str.into())
// - https://github.com/denoland/deno/issues/6649
// - https://github.com/v8/v8/blob/d68fb4733e39525f9ff0a9222107c02c28096e2a/include/v8.h#L3277-L3278
match v8::String::new_from_utf8(scope, &buf, v8::NewStringType::Normal) {
Some(text) => rv.set(text.into()),
None => {
let msg = v8::String::new(scope, "string too long").unwrap();
let exception = v8::Exception::range_error(scope, msg);
scope.throw_exception(exception);
}
};
}

fn queue_microtask(
Expand Down
4 changes: 2 additions & 2 deletions core/encode_decode_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ function main() {
Deno.core.decode(new Uint8Array(2 ** 29));
} catch (e) {
thrown = true;
assert(e instanceof TypeError);
assert(e.message === "Failed to decode");
assert(e instanceof RangeError);
assert(e.message === "string too long");
}
assert(thrown);
}
Expand Down

0 comments on commit 22a2bd8

Please sign in to comment.