svaarala / duktape

Duktape - embeddable Javascript engine with a focus on portability and compact footprint

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

webpacked "hello world" from Kotlin/JS erroring out with "empty expression not allowed"

dfontenot opened this issue · comments

Hello,

I am working on running some Kotlin/JS code under Duktape, and am running into this issue: SyntaxError: empty expression not allowed (line 1). I am using webpack to build a single .js file from the transpiled Kotlin code. I've observed this same error no matter what is in my .kt file, including even a simple hello world program.

To reproduce the issue:

main.kt

external fun print(input: Any)
fun main() {
    print("test from kotlin js")
}

Duktape 2.7.0 cmdline program output:

$ ./duk kotlin-test.js
SyntaxError: empty expression not allowed (line 1)
    at [anon] (kotlin-test.js:1) internal
    at [anon] (duk_js_compiler.c:4954) internal
error in executing file kotlin-test.js

Output from Node.JS version 18.4.1

$ node
Welcome to Node.js v18.14.1.
Type ".help" for more information.
> const print = (str) => console.log(str)
undefined
> require('./kotlin-test.js')
test from kotlin js
{}

Webpacked kotlin-test.js:

!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.openrct2_kotlinjs=e():t.openrct2_kotlinjs=e()}(this,(()=>{return t={295:function(t,e){var o,r;void 0===(r="function"==typeof(o=function(t){"use strict";return print("test from kotlin js"),t})?o.apply(e,[e]):o)||(t.exports=r)}},e={},function o(r){var n=e[r];if(void 0!==n)return n.exports;var p=e[r]={exports:{}};return t[r].call(p.exports,p,p.exports,o),p.exports}(295);var t,e}));

Am I doing something wrong with how I am generating the js bundle using webpack? I am just using the stock webpack setup in Kotlin/JS. Also, is there any general guidance on best webpack usage for targeting duktape?

Thanks.

Following, I get the same when webpacking TypeScript and executing in duktape.

I've moved to QuickJs and now JerryScript and don't have my DukTape project anymore, however it may be that DukTape doesn't like the 'Immediately Invoked Function Expression' which webpack creates.

I just read that IIFE can be disabled which may fix this?
https://webpack.js.org/configuration/output/#outputiife

It appears that IIFE was causing this. Workaround is listed below

I updated Kotlin, set kotlin.js.ir.output.granularity=whole-program, and tried again with a fresh project. New project included Webpack by default from the intellij project generator, and the webpacked result worked fine in duk.