2021-01-10 21:59:07 -05:00
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
2021-02-04 17:18:32 -05:00
"use strict" ;
2019-03-26 08:22:07 -04:00
2020-03-28 13:03:49 -04:00
( ( window ) => {
2019-08-05 07:23:41 -04:00
// Available on start due to bindings.
2021-04-23 11:50:45 -04:00
const { opcall } = window . Deno . core ;
2019-03-26 08:22:07 -04:00
2020-06-21 10:34:43 -04:00
let opsCache = { } ;
2021-05-03 11:30:41 -04:00
const errorMap = { } ;
// Builtin v8 / JS errors
registerErrorClass ( "Error" , Error ) ;
registerErrorClass ( "RangeError" , RangeError ) ;
registerErrorClass ( "ReferenceError" , ReferenceError ) ;
registerErrorClass ( "SyntaxError" , SyntaxError ) ;
registerErrorClass ( "TypeError" , TypeError ) ;
registerErrorClass ( "URIError" , URIError ) ;
2021-03-31 10:37:38 -04:00
let nextPromiseId = 1 ;
2021-04-07 08:38:54 -04:00
const promiseMap = new Map ( ) ;
const RING _SIZE = 4 * 1024 ;
const NO _PROMISE = null ; // Alias to null is faster than plain nulls
const promiseRing = new Array ( RING _SIZE ) . fill ( NO _PROMISE ) ;
2019-04-24 21:43:06 -04:00
2021-04-07 08:38:54 -04:00
function setPromise ( promiseId ) {
const idx = promiseId % RING _SIZE ;
// Move old promise from ring to map
const oldPromise = promiseRing [ idx ] ;
if ( oldPromise !== NO _PROMISE ) {
const oldPromiseId = promiseId - RING _SIZE ;
promiseMap . set ( oldPromiseId , oldPromise ) ;
}
// Set new promise
return promiseRing [ idx ] = newPromise ( ) ;
}
function getPromise ( promiseId ) {
// Check if out of ring bounds, fallback to map
const outOfBounds = promiseId < nextPromiseId - RING _SIZE ;
if ( outOfBounds ) {
const promise = promiseMap . get ( promiseId ) ;
promiseMap . delete ( promiseId ) ;
return promise ;
}
// Otherwise take from ring
const idx = promiseId % RING _SIZE ;
const promise = promiseRing [ idx ] ;
promiseRing [ idx ] = NO _PROMISE ;
return promise ;
}
function newPromise ( ) {
let resolve , reject ;
const promise = new Promise ( ( resolve _ , reject _ ) => {
resolve = resolve _ ;
reject = reject _ ;
} ) ;
promise . resolve = resolve ;
promise . reject = reject ;
return promise ;
}
2019-09-30 14:59:44 -04:00
function ops ( ) {
2021-03-31 10:37:38 -04:00
return opsCache ;
2019-03-14 19:17:52 -04:00
}
2021-04-25 16:00:05 -04:00
function syncOpsCache ( ) {
// op id 0 is a special value to retrieve the map of registered ops.
opsCache = Object . freeze ( Object . fromEntries ( opcall ( 0 ) ) ) ;
}
2021-03-31 10:37:38 -04:00
function handleAsyncMsgFromRust ( ) {
for ( let i = 0 ; i < arguments . length ; i += 2 ) {
2021-04-09 11:55:33 -04:00
const promiseId = arguments [ i ] ;
const res = arguments [ i + 1 ] ;
const promise = getPromise ( promiseId ) ;
promise . resolve ( res ) ;
2019-03-14 19:17:52 -04:00
}
}
2021-03-31 10:37:38 -04:00
function dispatch ( opName , promiseId , control , zeroCopy ) {
2021-04-23 11:50:45 -04:00
const opId = typeof opName === "string" ? opsCache [ opName ] : opName ;
return opcall ( opId , promiseId , control , zeroCopy ) ;
2019-03-14 19:17:52 -04:00
}
2021-04-21 20:50:50 -04:00
function registerErrorClass ( className , errorClass ) {
2021-05-03 11:30:41 -04:00
registerErrorBuilder ( className , ( msg ) => new errorClass ( msg ) ) ;
}
function registerErrorBuilder ( className , errorBuilder ) {
2021-04-21 20:50:50 -04:00
if ( typeof errorMap [ className ] !== "undefined" ) {
throw new TypeError ( ` Error class for " ${ className } " already registered ` ) ;
2021-03-20 12:51:08 -04:00
}
2021-05-03 11:30:41 -04:00
errorMap [ className ] = errorBuilder ;
2019-03-14 19:17:52 -04:00
}
2021-04-09 11:55:33 -04:00
function unwrapOpResult ( res ) {
// .$err_class_name is a special key that should only exist on errors
if ( res ? . $err _class _name ) {
const className = res . $err _class _name ;
2021-05-03 11:30:41 -04:00
const errorBuilder = errorMap [ className ] ;
if ( ! errorBuilder ) {
2021-04-09 11:55:33 -04:00
throw new Error (
` Unregistered error class: " ${ className } " \n ${ res . message } \n Classes of errors returned from ops should be registered via Deno.core.registerErrorClass(). ` ,
) ;
}
2021-05-03 11:30:41 -04:00
throw errorBuilder ( res . message ) ;
2020-08-07 16:47:18 -04:00
}
2021-04-09 11:55:33 -04:00
return res ;
2020-08-07 16:47:18 -04:00
}
2021-04-12 15:55:05 -04:00
function opAsync ( opName , args = null , zeroCopy = null ) {
2021-03-31 10:37:38 -04:00
const promiseId = nextPromiseId ++ ;
const maybeError = dispatch ( opName , promiseId , args , zeroCopy ) ;
// Handle sync error (e.g: error parsing args)
2021-04-09 11:55:33 -04:00
if ( maybeError ) return unwrapOpResult ( maybeError ) ;
return setPromise ( promiseId ) . then ( unwrapOpResult ) ;
2020-08-07 16:47:18 -04:00
}
2021-04-12 15:55:05 -04:00
function opSync ( opName , args = null , zeroCopy = null ) {
2021-04-09 11:55:33 -04:00
return unwrapOpResult ( dispatch ( opName , null , args , zeroCopy ) ) ;
2021-03-20 12:51:08 -04:00
}
2020-09-17 12:09:50 -04:00
function resources ( ) {
2021-04-12 15:55:05 -04:00
return Object . fromEntries ( opSync ( "op_resources" ) ) ;
2020-09-17 12:09:50 -04:00
}
2021-03-31 10:37:38 -04:00
2020-09-17 12:09:50 -04:00
function close ( rid ) {
2021-04-12 15:55:05 -04:00
opSync ( "op_close" , rid ) ;
2020-09-17 12:09:50 -04:00
}
2021-05-02 19:30:03 -04:00
function print ( str , isErr = false ) {
2021-05-15 09:22:57 -04:00
opSync ( "op_print" , str , isErr ) ;
2021-05-02 19:30:03 -04:00
}
2021-04-30 08:13:23 -04:00
// Provide bootstrap namespace
window . _ _bootstrap = { } ;
// Extra Deno.core.* exports
2020-05-12 11:09:28 -04:00
Object . assign ( window . Deno . core , {
2021-04-12 15:55:05 -04:00
opAsync ,
opSync ,
2020-05-12 11:09:28 -04:00
ops ,
2020-09-17 12:09:50 -04:00
close ,
2021-05-02 19:30:03 -04:00
print ,
2020-09-17 12:09:50 -04:00
resources ,
2021-05-03 11:30:41 -04:00
registerErrorBuilder ,
2020-08-07 16:47:18 -04:00
registerErrorClass ,
2021-04-21 20:48:17 -04:00
handleAsyncMsgFromRust ,
2021-04-25 16:00:05 -04:00
syncOpsCache ,
2020-05-12 11:09:28 -04:00
} ) ;
2019-04-24 21:43:06 -04:00
} ) ( this ) ;