Skip to content

Commit

Permalink
fix(crypto): change Crypto to interface (denoland#10853)
Browse files Browse the repository at this point in the history
Co-authored-by: Luca Casonato <[email protected]>
  • Loading branch information
crowlKats and lucacasonato committed Jun 5, 2021
1 parent 368c784 commit bb0c90c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 46 deletions.
84 changes: 49 additions & 35 deletions extensions/crypto/01_crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,62 @@

((window) => {
const core = window.Deno.core;
const webidl = window.__bootstrap.webidl;

function getRandomValues(arrayBufferView) {
if (!ArrayBuffer.isView(arrayBufferView)) {
throw new TypeError(
"Argument 1 does not implement interface ArrayBufferView",
);
class Crypto {
constructor() {
webidl.illegalConstructor();
}
if (
!(
arrayBufferView instanceof Int8Array ||
arrayBufferView instanceof Uint8Array ||
arrayBufferView instanceof Int16Array ||
arrayBufferView instanceof Uint16Array ||
arrayBufferView instanceof Int32Array ||
arrayBufferView instanceof Uint32Array ||
arrayBufferView instanceof Uint8ClampedArray
)
) {
throw new DOMException(
"The provided ArrayBufferView is not an integer array type",
"TypeMismatchError",

getRandomValues(arrayBufferView) {
webidl.assertBranded(this, Crypto);
const prefix = "Failed to execute 'getRandomValues' on 'Crypto'";
webidl.requiredArguments(arguments.length, 1, { prefix });
arrayBufferView = webidl.converters.ArrayBufferView(arrayBufferView, {
prefix,
context: "Argument 1",
});
if (
!(
arrayBufferView instanceof Int8Array ||
arrayBufferView instanceof Uint8Array ||
arrayBufferView instanceof Int16Array ||
arrayBufferView instanceof Uint16Array ||
arrayBufferView instanceof Int32Array ||
arrayBufferView instanceof Uint32Array ||
arrayBufferView instanceof Uint8ClampedArray
)
) {
throw new DOMException(
"The provided ArrayBufferView is not an integer array type",
"TypeMismatchError",
);
}
const ui8 = new Uint8Array(
arrayBufferView.buffer,
arrayBufferView.byteOffset,
arrayBufferView.byteLength,
);
core.opSync("op_crypto_get_random_values", ui8);
return arrayBufferView;
}

randomUUID() {
webidl.assertBranded(this, Crypto);
return core.opSync("op_crypto_random_uuid");
}
const ui8 = new Uint8Array(
arrayBufferView.buffer,
arrayBufferView.byteOffset,
arrayBufferView.byteLength,
);
core.opSync("op_crypto_get_random_values", ui8);
return arrayBufferView;
}

function randomUUID() {
return core.opSync("op_crypto_random_uuid");
get [Symbol.toStringTag]() {
return "Crypto";
}

[Symbol.for("Deno.customInspect")](inspect) {
return `${this.constructor.name} ${inspect({})}`;
}
}

window.crypto = {
getRandomValues,
randomUUID,
};
window.__bootstrap.crypto = {
getRandomValues,
randomUUID,
crypto: webidl.createBranded(Crypto),
Crypto,
};
})(this);
3 changes: 2 additions & 1 deletion runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ delete Object.prototype.__proto__;
console: util.writable(
new Console((msg, level) => core.print(msg, level > 1)),
),
crypto: util.readOnly(crypto),
crypto: util.readOnly(crypto.crypto),
Crypto: util.nonEnumerable(crypto.Crypto),
fetch: util.writable(fetch.fetch),
performance: util.writable(performance.performance),
setInterval: util.writable(timers.setInterval),
Expand Down
10 changes: 0 additions & 10 deletions tools/wpt/expectation.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,9 @@
},
"historical.any.html": true,
"idlharness.https.any.html": [
"Crypto interface: existence and properties of interface object",
"Crypto interface object length",
"Crypto interface object name",
"Crypto interface: existence and properties of interface prototype object",
"Crypto interface: existence and properties of interface prototype object's \"constructor\" property",
"Crypto interface: existence and properties of interface prototype object's @@unscopables property",
"Crypto interface: attribute subtle",
"Crypto interface: operation getRandomValues(ArrayBufferView)",
"Crypto must be primary interface of crypto",
"Stringification of crypto",
"Crypto interface: crypto must inherit property \"subtle\" with the proper type",
"Crypto interface: crypto must inherit property \"getRandomValues(ArrayBufferView)\" with the proper type",
"Crypto interface: calling getRandomValues(ArrayBufferView) on crypto with too few arguments must throw TypeError",
"CryptoKey interface: existence and properties of interface object",
"CryptoKey interface object length",
"CryptoKey interface object name",
Expand Down

0 comments on commit bb0c90c

Please sign in to comment.