denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
95.42k stars 5.29k forks source link

Starting REPLServer manually #6311

Open kungfooman opened 4 years ago

kungfooman commented 4 years ago

Node has: https://nodejs.org/api/repl.html#repl_repl_start_options

So I can simply start a webserver and spawn a REPL in the same context to manipulate the server object for example:

require("repl").start("custom repl>");

How can I do the same in deno?

bartlomieju commented 4 years ago

There's no way to do that currently.

kungfooman commented 4 years ago

I asked on Discord and @SyrupThinker shared his attempt (thanks a lot ❤️):

https://gist.githubusercontent.com/SyrupThinker/bdc1e7a3bf359745908be8f93db34830/raw/9139335b0a66b7aa16b180c83b26d72595a03436/repl.ts

Usage like:

// Filename: test_repl.ts
import * as repl from "./repl.ts";
Object.assign(window, {
    i: 42
});
await repl.replLoop();

Only problem so far seems to be that it crashes when objects are too "complex" (e.g. type in Deno itself), then it requires:

deno run --allow-read test_repl.ts

Otherwise it will fail like this:

image

Deno 1.1.0
exit using ctrl+d or close()
> Deno
error: Uncaught PermissionDenied: read access to <main_module>, run again with the --allow-read flag
    at unwrapResponse ($deno$/ops/dispatch_json.ts:43:11)
    at Object.sendSync ($deno$/ops/dispatch_json.ts:72:10)
    at Object.opMainModule [as mainModule] ($deno$/ops/runtime.ts:25:10)
    at createRawObjectString ($deno$/web/console.ts:512:43)
    at createObjectString ($deno$/web/console.ts:592:12)
    at stringify ($deno$/web/console.ts:296:14)
    at stringifyArgs ($deno$/web/console.ts:685:14)
    at Object.Console.log ($deno$/web/console.ts:729:7)
    at replLog (file:///D:/web/libwebgame_node/src/repl_deno.ts:57:11)
    at evaluate (file:///D:/web/libwebgame_node/src/repl_deno.ts:109:7)
SyrupThinker commented 4 years ago

As noted when I shared this gist, this heavily relies on internal behaviour and might break at any release.

caspervonb commented 4 years ago

As noted when I shared this gist, this heavily relies on internal behaviour and might break at any release.

As of https://github.com/denoland/deno/commit/4c779b5e8ca427faf24c26443a8054004827d450 there are no repl ops and the runtime environment has no concept of a repl at all.