Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eval() do not support TypeScript. #8256

Closed
Ayfri opened this issue Nov 5, 2020 · 10 comments
Closed

eval() do not support TypeScript. #8256

Ayfri opened this issue Nov 5, 2020 · 10 comments

Comments

@Ayfri
Copy link

Ayfri commented Nov 5, 2020

You can't use any typescript features in the native eval() function, you can't use import/export also, it would be great to be able to use these.

@RDambrosio016
Copy link

this would probably require overriding the native eval to first detect if its typescript, if so, then start up the entirety of tsc, compile the javascript, ensure there's no errors, then give it to v8 to actually run it. This would incur a huge performance loss because tsc is relatively slow. I don't see benefits in ever allowing this because of that.

@Ayfri
Copy link
Author

Ayfri commented Nov 5, 2020

You could add an option eval(code: string, options?: { ts?: boolean }); like this ?
Or a Deno.eval() that will be different than Deno.run() as it will be run in the local scope.

@RDambrosio016
Copy link

This would also not work correctly for type checking because eval can grab variables from its scope but the tsc checker does not know about the outer context and eval is invoked at runtime

@Ayfri
Copy link
Author

Ayfri commented Nov 5, 2020

And it would require a lot of time to find a way to make it work I suppose?
Can't you "simulate" the variables by declaring them when compiling?

@RDambrosio016
Copy link

You would have to know what variables are declared in the scope, so either plug into the tsc type checker api, which is complicated, or get it from v8 somehow, also complicated. Overall i don't think the drawbacks outweight the pros:

  • Huge performance cost
  • Hard to implement correctly
  • Most likely causes a lot of technical debt

@Ayfri
Copy link
Author

Ayfri commented Nov 5, 2020

Okay, it's too bad...

@Ayfri Ayfri closed this as completed Nov 5, 2020
@thgh
Copy link
Contributor

thgh commented Dec 29, 2020

@Ayfri If you wanted to use import/export, this is a step forwards:

const mod = await tseval(`
  import { red } from "https://deno.land/[email protected]/fmt/colors.ts";
  export function echo(message: string) {
    return red(message)
  }
`)

console.log(mod.echo('hello'))

async function tseval(code: string) {
  const moduleURL = import.meta.url.replace('file://', '') + '$eval.ts'
  await Deno.writeFile(moduleURL, new TextEncoder().encode(code))
  return import(moduleURL)
}

@Ayfri
Copy link
Author

Ayfri commented Dec 29, 2020

Oh, that is neat!
Thank you!

@Ayfri
Copy link
Author

Ayfri commented Dec 30, 2020

I'm getting an error :

Error: The syntax of the file, directory, or volume name is incorrect. (os error 123)
    at processResponse (deno:core/core.js:223:11)
    at Object.jsonOpAsync (deno:core/core.js:240:12)
    at async open (deno:runtime/js/30_files.js:44:17)
    at async Object.writeFile (deno:runtime/js/40_write_file.js:54:18)
    at async tseval (mod.ts:5:2)

@thgh
Copy link
Contributor

thgh commented Jan 6, 2021

If you want to use that code from a remote module, you will need to change the moduleURL to a writable path.

But when #5059 is released, it will get easier:

function tseval (code: string) {
  return import('data:application/javascript,' + encodeURIComponent(code));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants