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

TypeScript compatibility #523

Closed
fatcerberus opened this Issue Jan 11, 2016 · 27 comments

Comments

Projects
None yet
5 participants
@fatcerberus
Copy link
Contributor

fatcerberus commented Jan 11, 2016

Is Duktape compatible with TypeScript? The Guide mentions CoffeeScript and few other "transpilers", but no mention of TypeScript. Is it just that no formal testing been done with this?

@svaarala

This comment has been minimized.

Copy link
Owner

svaarala commented Jan 11, 2016

There are a couple of people at least running TypeScript with Duktape, so it should be quite functional. It should be added to the website, but it'd be nice someone working with TypeScript + Duktape could comment on if there are any issues.

@JoshEngebretson

This comment has been minimized.

Copy link

JoshEngebretson commented Jan 11, 2016

TypeScript is used in the AtomicEditor, here's a sample script:

https://github.com/AtomicGameEngine/AtomicGameEngine/blob/master/Script/AtomicEditor/ui/frames/inspector/AttributeInfoEdit.ts

There is also support for TypeScript in projects (we're working to streamline this) :

https://github.com/AtomicGameEngine/AtomicExamples/tree/master/Roboman3DTypeScript

We haven't encountered a single Duktape related issue with the transpiled JS to date.

@fatcerberus

This comment has been minimized.

Copy link
Contributor Author

fatcerberus commented Jan 11, 2016

How about the TypeScript transpiler itself? Do you run that with Duktape, or is the TS code transpiled through some other mechanism?

@JoshEngebretson

This comment has been minimized.

Copy link

JoshEngebretson commented Jan 11, 2016

@fatcerberus We use node + tsc for compiling, I haven't run the TS compiler on Duktape. This would likely work, though require some shims for a few trivial node API's for IO

@fatcerberus

This comment has been minimized.

Copy link
Contributor Author

fatcerberus commented Jan 11, 2016

Thanks guys. :)

@JoshEngebretson

This comment has been minimized.

Copy link

JoshEngebretson commented Jan 16, 2016

We have now run the TypeScript compiler under Duktape!

Microsoft has made it easy to plug in IO handling, etc into the compiler. Straight transpiling is pretty fast, full on compiles (with full validation) are slower.

We're still working on the integration and can provide better performance metrics. Though, it works and only required relaxing some regex 👍

@svaarala

This comment has been minimized.

Copy link
Owner

svaarala commented Jan 16, 2016

@JoshEngebretson Cool :) If those regexp issues were curly brace related, an incoming pull might make those fixes unnecessary.

@JoshEngebretson

This comment has been minimized.

Copy link

JoshEngebretson commented Jan 16, 2016

@svaarala I wasn't the one who got it working, though there should be a PR soon with more details on the regex tweak. I was just really excited that it works 😆

@svaarala

This comment has been minimized.

Copy link
Owner

svaarala commented Jan 16, 2016

I was referring to @crazyjul's pull (#513) which would allow unescaped curly braces. That's perhaps the most common issue :)

@svaarala

This comment has been minimized.

Copy link
Owner

svaarala commented Jan 16, 2016

@JoshEngebretson Anyway it's great that you got it working :)

@shaddockh

This comment has been minimized.

Copy link

shaddockh commented Jan 17, 2016

I have an update to this. To get typescript.js to run under duktape, it actually was related to unescaped curly braces. The only change I needed to make was to escape the curly braces on line 1171:

return text.replace(/\{(\d+)\}/g, function (match, index) { return args[+index + baseIndex]; });

I've also run into the same issue bringing rot.js ( https://github.com/ondras/rot.js ) into duktape. All the changes I had to make on that were related to curly brace escaping.

So, if curly brace escaping is no longer necessary, that would be awesome!!

@fatcerberus

This comment has been minimized.

Copy link
Contributor Author

fatcerberus commented Jan 17, 2016

Yeah, once #513 is merged such edits won't be needed anymore. It seems literal braces in regexps is very common despite violating the ES spec (?), if even something as high-profile as the TypeScript compiler expects them to work.

@svaarala

This comment has been minimized.

Copy link
Owner

svaarala commented Jan 17, 2016

@fatcerberus At least from my reading of the specification literal braces are excluded from pattern characters:

PatternCharacter ::
    SourceCharacter but not one of
    ^ $ \ . * + ? ( ) [ ] { } |
@shaddockh

This comment has been minimized.

Copy link

shaddockh commented Jan 17, 2016

fyi, I just confirmed that I was able to load an unmodified copy of typescript.js into the Duktape vm with these changes applied. Thanks!!

@svaarala

This comment has been minimized.

Copy link
Owner

svaarala commented Jan 17, 2016

Good to hear, thanks :)

@svaarala

This comment has been minimized.

Copy link
Owner

svaarala commented Jan 17, 2016

Btw, could I link to something in the Atomic repo to provide an example of how to run the TypeScript compiler with Duktape?

@fatcerberus

This comment has been minimized.

Copy link
Contributor Author

fatcerberus commented Jan 17, 2016

Oh goody, now I can go ahead with TypeScript integration for minisphere 3.0. Thanks, @crazyjul!

@JoshEngebretson

This comment has been minimized.

Copy link

JoshEngebretson commented Jan 17, 2016

@svaarala We're still working out how compiling TS from the editor is going to work, this isn't a simple example, though if you're curious:

https://github.com/shaddockh/AtomicGameEngine/blob/TSH-ATOMIC-TYPESCRIPT/Script/AtomicEditor/extensionServices/resourceServices/TypescriptLanguageService.ts

The TypeScript compiler can just work with string buffers, so you could probably add a simple/minimal example pretty easily...

@JoshEngebretson

This comment has been minimized.

Copy link

JoshEngebretson commented Jan 19, 2016

Speaking of TypeScript, checkout Atomic's new code editor via Ace and the embedded UIWebView

Themed syntax highlighting for pretty much every common language, code folding, autocompletion, even linting with info boxes in the gutter!

Going to be fun hooking up Duktape's debugger with it :)

atomicts

@fatcerberus

This comment has been minimized.

Copy link
Contributor Author

fatcerberus commented Jan 19, 2016

Looks nice. As for debugger integration, I recommend trying to adapt the JSON proxy if at all possible. I used the binary protocol directly for minisphere to keep my dependencies to a minimum (JSON proxy requires node.js), but it's pretty tedious to maintain.

@svaarala

This comment has been minimized.

Copy link
Owner

svaarala commented Jan 19, 2016

Just to provide some background: the debugger protocol and the debugger API are quite low level; they're that way to allow Duktape debugger to work with in very low memory environments (it can work with less than 1kB of static memory overhead and no dynamic allocations in many cases). The downside is that it's binary, etc.

That low memory target is very important because low level environments are one area where Duktape has a lot of (current and potential) users. There are some steps that we can take to make the low level integration (binary protocol) easier to work with, but only up to a certain limit.

So while the low level protocol is binary, I'd like to provide (at least) three additional components (outside of the target) to make integration easier, without compromising the low memory targets:

  • A JSON mapping and a JSON proxy which talks binary to the target and JSON to you. There's a Node.js based JSON proxy in the master already, so this is working, but perhaps the Node.js dependency is an overkill. (We could write one using Dukluv... :-)
  • A C/C++ library running on the remote peer (debug client) which would allow you to talk to the target without needing to read/write the binary messages directly. Similar to the JSON proxy but working inside the remote peer process. This is currently missing, though https://github.com/svaarala/duktape/tree/master/examples/debug-trans-dvalue should be relatively easy to adapt for that purpose.
  • A Node.js library running on the remote peer to talk to the target. There's a Node.js based debug protocol handling class in the debugger example which ships with Duktape. That should be relatively easy to adapt to provide a Node.js API for debugger integration, but at the moment it's tied to the debugger example (not very deeply but still).

@JoshEngebretson When you get around to decide on debugger integration, let me know how you see the various alternatives. I can chip in to make the process easier if you'd prefer a native library or something similar.

@JoshEngebretson

This comment has been minimized.

Copy link

JoshEngebretson commented Jan 19, 2016

Thanks guys, @svaarala great info dump (as always). I'll be looking at this sooner than later, quite requested feature... I have a good amount of experience with Lua's low level debug interface so hopefully that along with this advice/info will help :)

@fatcerberus

This comment has been minimized.

Copy link
Contributor Author

fatcerberus commented Jan 19, 2016

Lua has a debugger? You learn something new every day. :)

@ghost

This comment has been minimized.

Copy link

ghost commented Jan 29, 2016

It could be easy to write a tiny debugger using a stand-alone GUI app, in Qt for example. Well, this is something I need to code in my growing TODO list :)

@thradams

This comment has been minimized.

Copy link

thradams commented Apr 19, 2018

I tested typescriptServices.js with duktape.

Error I got is:
ReferenceError: identifier 'ts' undefined

int main()
{
  duk_context *ctx = duk_create_heap_default();
  
  fileio_push_file_string(ctx, "typescriptServices.js");
  if (duk_peval(ctx) != 0)
  {
    const char* pError = duk_safe_to_string(ctx, -1);
  }
  else
  {
    if (duk_peval_string(ctx, "ts.transpile('class X{}');") != 0) 
    {
      const char* pError = duk_safe_to_string(ctx, -1);
      pError = 0;
    }
  }
  
  printf("return value is: %s\n", duk_get_string(ctx, -1));
  duk_destroy_heap(ctx);
  return 0;
}
@svaarala

This comment has been minimized.

Copy link
Owner

svaarala commented Apr 19, 2018

Could you print the error .stack so there's a better idea of the line number etc?

@svaarala

This comment has been minimized.

Copy link
Owner

svaarala commented Apr 19, 2018

FWIW in master:

duktape@leijulauta:~/duktape$ ./duk /tmp/typescriptServices.js  -i
duk> ts.transpile('class X{}');
= "var X = /** @class */ (function () {\r\n    function X() {\r\n    }\r\n    return X;\r\n}());\r\n"
duk> 
Cleaning up...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment