diff options
Diffstat (limited to 'js')
-rw-r--r-- | js/compiler.ts | 56 | ||||
-rw-r--r-- | js/dispatch.ts | 17 | ||||
-rw-r--r-- | js/error_stack.ts | 51 | ||||
-rw-r--r-- | js/fetch.ts | 96 | ||||
-rw-r--r-- | js/files.ts | 52 | ||||
-rw-r--r-- | js/format_error.ts | 18 |
6 files changed, 90 insertions, 200 deletions
diff --git a/js/compiler.ts b/js/compiler.ts index 7519c5115..5399d59ad 100644 --- a/js/compiler.ts +++ b/js/compiler.ts @@ -7,9 +7,11 @@ import { Console } from "./console"; import { core } from "./core"; import { Diagnostic, fromTypeScriptDiagnostic } from "./diagnostics"; import { cwd } from "./dir"; -import { sendSync, msg, flatbuffers } from "./dispatch_flatbuffers"; +import * as dispatch from "./dispatch"; +import { sendSync } from "./dispatch_json"; +import { msg } from "./dispatch_flatbuffers"; import * as os from "./os"; -import { TextDecoder, TextEncoder } from "./text_encoding"; +import { TextEncoder } from "./text_encoding"; import { getMappedModuleName, parseTypeDirectives } from "./type_directives"; import { assert, notImplemented } from "./util"; import * as util from "./util"; @@ -121,35 +123,15 @@ interface EmitResult { /** Ops to Rust to resolve and fetch a modules meta data. */ function fetchSourceFile(specifier: string, referrer: string): SourceFile { - util.log("fetchSourceFile", { specifier, referrer }); - // Send FetchSourceFile message - const builder = flatbuffers.createBuilder(); - const specifier_ = builder.createString(specifier); - const referrer_ = builder.createString(referrer); - const inner = msg.FetchSourceFile.createFetchSourceFile( - builder, - specifier_, - referrer_ - ); - const baseRes = sendSync(builder, msg.Any.FetchSourceFile, inner); - assert(baseRes != null); - assert( - msg.Any.FetchSourceFileRes === baseRes!.innerType(), - `base.innerType() unexpectedly is ${baseRes!.innerType()}` - ); - const fetchSourceFileRes = new msg.FetchSourceFileRes(); - assert(baseRes!.inner(fetchSourceFileRes) != null); - const dataArray = fetchSourceFileRes.dataArray(); - const decoder = new TextDecoder(); - const sourceCode = dataArray ? decoder.decode(dataArray) : undefined; - // flatbuffers returns `null` for an empty value, this does not fit well with - // idiomatic TypeScript under strict null checks, so converting to `undefined` + util.log("compiler.fetchSourceFile", { specifier, referrer }); + const res = sendSync(dispatch.OP_FETCH_SOURCE_FILE, { + specifier, + referrer + }); + return { - moduleName: fetchSourceFileRes.moduleName() || undefined, - filename: fetchSourceFileRes.filename() || undefined, - mediaType: fetchSourceFileRes.mediaType(), - sourceCode, - typeDirectives: parseTypeDirectives(sourceCode) + ...res, + typeDirectives: parseTypeDirectives(res.sourceCode) }; } @@ -171,19 +153,7 @@ function humanFileSize(bytes: number): string { /** Ops to rest for caching source map and compiled js */ function cache(extension: string, moduleId: string, contents: string): void { - util.log("cache", extension, moduleId); - const builder = flatbuffers.createBuilder(); - const extension_ = builder.createString(extension); - const moduleId_ = builder.createString(moduleId); - const contents_ = builder.createString(contents); - const inner = msg.Cache.createCache( - builder, - extension_, - moduleId_, - contents_ - ); - const baseRes = sendSync(builder, msg.Any.Cache, inner); - assert(baseRes == null); + sendSync(dispatch.OP_CACHE, { extension, moduleId, contents }); } const encoder = new TextEncoder(); diff --git a/js/dispatch.ts b/js/dispatch.ts index d1da45b06..b59274f91 100644 --- a/js/dispatch.ts +++ b/js/dispatch.ts @@ -15,6 +15,14 @@ export const OP_UTIME = 7; export const OP_SET_ENV = 8; export const OP_HOME_DIR = 9; export const OP_START = 10; +export const OP_APPLY_SOURCE_MAP = 11; +export const OP_FORMAT_ERROR = 12; +export const OP_CACHE = 13; +export const OP_FETCH_SOURCE_FILE = 14; +export const OP_OPEN = 15; +export const OP_CLOSE = 16; +export const OP_SEEK = 17; +export const OP_FETCH = 18; export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void { switch (opId) { @@ -25,10 +33,17 @@ export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void { case OP_READ: minimal.asyncMsgFromRust(opId, ui8); break; + case OP_EXIT: + case OP_IS_TTY: + case OP_ENV: + case OP_EXEC_PATH: case OP_UTIME: + case OP_OPEN: + case OP_SEEK: + case OP_FETCH: json.asyncMsgFromRust(opId, ui8); break; default: - throw Error("bad opId"); + throw Error("bad async opId"); } } diff --git a/js/error_stack.ts b/js/error_stack.ts index 003717a72..7d41b9cf4 100644 --- a/js/error_stack.ts +++ b/js/error_stack.ts @@ -1,8 +1,8 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Some of the code here is adapted directly from V8 and licensed under a BSD // style license available here: https://github.com/v8/v8/blob/24886f2d1c565287d33d71e4109a53bf0b54b75c/LICENSE.v8 - -import { sendSync, msg, flatbuffers } from "./dispatch_flatbuffers"; +import * as dispatch from "./dispatch"; +import { sendSync } from "./dispatch_json"; import { assert } from "./util"; export interface Location { @@ -17,40 +17,6 @@ export interface Location { column: number; } -function req( - filename: string, - line: number, - column: number -): [flatbuffers.Builder, msg.Any.ApplySourceMap, flatbuffers.Offset] { - const builder = flatbuffers.createBuilder(); - const filename_ = builder.createString(filename); - const inner = msg.ApplySourceMap.createApplySourceMap( - builder, - filename_, - // On this side, line/column are 1 based, but in the source maps, they are - // 0 based, so we have to convert back and forth - line - 1, - column - 1 - ); - return [builder, msg.Any.ApplySourceMap, inner]; -} - -function res(baseRes: msg.Base | null): Location { - assert(baseRes != null); - assert(baseRes!.innerType() === msg.Any.ApplySourceMap); - const res = new msg.ApplySourceMap(); - assert(baseRes!.inner(res) != null); - const filename = res.filename()!; - assert(filename != null); - return { - filename, - // On this side, line/column are 1 based, but in the source maps, they are - // 0 based, so we have to convert back and forth - line: res.line() + 1, - column: res.column() + 1 - }; -} - /** Given a current location in a module, lookup the source location and * return it. * @@ -75,7 +41,18 @@ function res(baseRes: msg.Base | null): Location { */ export function applySourceMap(location: Location): Location { const { filename, line, column } = location; - return res(sendSync(...req(filename, line, column))); + // On this side, line/column are 1 based, but in the source maps, they are + // 0 based, so we have to convert back and forth + const res = sendSync(dispatch.OP_APPLY_SOURCE_MAP, { + filename, + line: line - 1, + column: column - 1 + }); + return { + filename: res.filename, + line: res.line + 1, + column: res.column + 1 + }; } /** Mutate the call site so that it returns the location, instead of its diff --git a/js/fetch.ts b/js/fetch.ts index 505da218c..317239630 100644 --- a/js/fetch.ts +++ b/js/fetch.ts @@ -1,6 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import { assert, createResolvable, notImplemented, isTypedArray } from "./util"; -import { sendAsync, msg, flatbuffers } from "./dispatch_flatbuffers"; import * as domTypes from "./dom_types"; import { TextDecoder, TextEncoder } from "./text_encoding"; import { DenoBlob, bytesSymbol as blobBytesSymbol } from "./blob"; @@ -10,6 +9,8 @@ import { read, close } from "./files"; import { Buffer } from "./buffer"; import { FormData } from "./form_data"; import { URLSearchParams } from "./url_search_params"; +import * as dispatch from "./dispatch"; +import { sendAsync } from "./dispatch_json"; function getHeaderValueParams(value: string): Map<string, string> { const params = new Map(); @@ -320,67 +321,35 @@ export class Response implements domTypes.Response { } } -function msgHttpRequest( - builder: flatbuffers.Builder, - url: string, - method: null | string, - headers: null | domTypes.Headers -): flatbuffers.Offset { - const methodOffset = !method ? 0 : builder.createString(method); - let fieldsOffset: flatbuffers.Offset = 0; - const urlOffset = builder.createString(url); - if (headers) { - const kvOffsets: flatbuffers.Offset[] = []; - for (const [key, val] of headers.entries()) { - const keyOffset = builder.createString(key); - const valOffset = builder.createString(val); - kvOffsets.push( - msg.KeyValue.createKeyValue(builder, keyOffset, valOffset) - ); - } - fieldsOffset = msg.HttpHeader.createFieldsVector(builder, kvOffsets); - } else { - } - return msg.HttpHeader.createHttpHeader( - builder, - true, - methodOffset, - urlOffset, - 0, - fieldsOffset - ); -} - -function deserializeHeaderFields(m: msg.HttpHeader): Array<[string, string]> { - const out: Array<[string, string]> = []; - for (let i = 0; i < m.fieldsLength(); i++) { - const item = m.fields(i)!; - out.push([item.key()!, item.value()!]); - } - return out; +interface FetchResponse { + bodyRid: number; + status: number; + headers: Array<[string, string]>; } -async function getFetchRes( +async function sendFetchReq( url: string, method: string | null, headers: domTypes.Headers | null, body: ArrayBufferView | undefined -): Promise<msg.FetchRes> { - // Send Fetch message - const builder = flatbuffers.createBuilder(); - const headerOff = msgHttpRequest(builder, url, method, headers); - const resBase = await sendAsync( - builder, - msg.Any.Fetch, - msg.Fetch.createFetch(builder, headerOff), - body - ); - - // Decode FetchRes - assert(msg.Any.FetchRes === resBase.innerType()); - const inner = new msg.FetchRes(); - assert(resBase.inner(inner) != null); - return inner; +): Promise<FetchResponse> { + let headerArray: Array<[string, string]> = []; + if (headers) { + headerArray = Array.from(headers.entries()); + } + + let zeroCopy = undefined; + if (body) { + zeroCopy = new Uint8Array(body.buffer, body.byteOffset, body.byteLength); + } + + const args = { + method, + url, + headers: headerArray + }; + + return (await sendAsync(dispatch.OP_FETCH, args, zeroCopy)) as FetchResponse; } /** Fetch a resource from the network. */ @@ -448,20 +417,13 @@ export async function fetch( } while (remRedirectCount) { - const inner = await getFetchRes(url, method, headers, body); - - const header = inner.header()!; - const bodyRid = inner.bodyRid(); - assert(!header.isRequest()); - const status = header.status(); - - const headersList = deserializeHeaderFields(header); + const fetchResponse = await sendFetchReq(url, method, headers, body); const response = new Response( url, - status, - headersList, - bodyRid, + fetchResponse.status, + fetchResponse.headers, + fetchResponse.bodyRid, redirected ); if ([301, 302, 303, 307, 308].includes(response.status)) { diff --git a/js/files.ts b/js/files.ts index 6f0e523c9..4eff17aac 100644 --- a/js/files.ts +++ b/js/files.ts @@ -12,37 +12,22 @@ import { } from "./io"; import { sendAsyncMinimal } from "./dispatch_minimal"; import { assert } from "./util"; -import { sendAsync, sendSync, msg, flatbuffers } from "./dispatch_flatbuffers"; +import * as dispatch from "./dispatch"; +import { + sendSync as sendSyncJson, + sendAsync as sendAsyncJson +} from "./dispatch_json"; +import { sendSync, msg, flatbuffers } from "./dispatch_flatbuffers"; import { OP_READ, OP_WRITE } from "./dispatch"; -function reqOpen( - filename: string, - mode: OpenMode -): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] { - const builder = flatbuffers.createBuilder(); - const filename_ = builder.createString(filename); - const mode_ = builder.createString(mode); - const inner = msg.Open.createOpen(builder, filename_, 0, mode_); - return [builder, msg.Any.Open, inner]; -} - -function resOpen(baseRes: null | msg.Base): File { - assert(baseRes != null); - assert(msg.Any.OpenRes === baseRes!.innerType()); - const res = new msg.OpenRes(); - assert(baseRes!.inner(res) != null); - const rid = res.rid(); - // eslint-disable-next-line @typescript-eslint/no-use-before-define - return new File(rid); -} - /** Open a file and return an instance of the `File` object * synchronously. * * const file = Deno.openSync("/foo/bar.txt"); */ export function openSync(filename: string, mode: OpenMode = "r"): File { - return resOpen(sendSync(...reqOpen(filename, mode))); + const rid = sendSyncJson(dispatch.OP_OPEN, { filename, mode }); + return new File(rid); } /** Open a file and return an instance of the `File` object. @@ -55,7 +40,8 @@ export async function open( filename: string, mode: OpenMode = "r" ): Promise<File> { - return resOpen(await sendAsync(...reqOpen(filename, mode))); + const rid = await sendAsyncJson(dispatch.OP_OPEN, { filename, mode }); + return new File(rid); } function reqRead( @@ -165,23 +151,13 @@ export async function write(rid: number, p: Uint8Array): Promise<number> { } } -function reqSeek( - rid: number, - offset: number, - whence: SeekMode -): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] { - const builder = flatbuffers.createBuilder(); - const inner = msg.Seek.createSeek(builder, rid, offset, whence); - return [builder, msg.Any.Seek, inner]; -} - /** Seek a file ID synchronously to the given offset under mode given by `whence`. * * const file = Deno.openSync("/foo/bar.txt"); * Deno.seekSync(file.rid, 0, 0); */ export function seekSync(rid: number, offset: number, whence: SeekMode): void { - sendSync(...reqSeek(rid, offset, whence)); + sendSyncJson(dispatch.OP_SEEK, { rid, offset, whence }); } /** Seek a file ID to the given offset under mode given by `whence`. @@ -196,14 +172,12 @@ export async function seek( offset: number, whence: SeekMode ): Promise<void> { - await sendAsync(...reqSeek(rid, offset, whence)); + await sendAsyncJson(dispatch.OP_SEEK, { rid, offset, whence }); } /** Close the file ID. */ export function close(rid: number): void { - const builder = flatbuffers.createBuilder(); - const inner = msg.Close.createClose(builder, rid); - sendSync(builder, msg.Any.Close, inner); + sendSyncJson(dispatch.OP_CLOSE, { rid }); } /** The Deno abstraction for reading and writing files. */ diff --git a/js/format_error.ts b/js/format_error.ts index 6670b05e2..dde0f6a58 100644 --- a/js/format_error.ts +++ b/js/format_error.ts @@ -1,17 +1,9 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { sendSync, msg, flatbuffers } from "./dispatch_flatbuffers"; -import { assert } from "./util"; +import * as dispatch from "./dispatch"; +import { sendSync } from "./dispatch_json"; +// TODO(bartlomieju): move to `repl.ts`? export function formatError(errString: string): string { - const builder = flatbuffers.createBuilder(); - const errString_ = builder.createString(errString); - const offset = msg.FormatError.createFormatError(builder, errString_); - const baseRes = sendSync(builder, msg.Any.FormatError, offset); - assert(baseRes != null); - assert(msg.Any.FormatErrorRes === baseRes!.innerType()); - const formatErrorResMsg = new msg.FormatErrorRes(); - assert(baseRes!.inner(formatErrorResMsg) != null); - const formattedError = formatErrorResMsg.error(); - assert(formatError != null); - return formattedError!; + const res = sendSync(dispatch.OP_FORMAT_ERROR, { error: errString }); + return res.error; } |