Skip to content

js.Dynamic.literal does not generate a JS object literal with Scala 2.13/3. #5017

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

Open
trival opened this issue Aug 19, 2024 · 0 comments
Open
Assignees
Labels
optimization Optimization only. Does not affect semantics or correctness.

Comments

@trival
Copy link

trival commented Aug 19, 2024

I have this Scala file object-literal.scala :

//> using scala 3.4.2
//> using jsVersion 1.16.0
//> using jsMode full
//> using jsModuleKind es
//> using dep org.scala-js:scalajs-library_2.13:1.16.0

import scala.scalajs.js.annotation.*
import scala.scalajs.js

@JSExportTopLevel("testObj1")
val testObj1 = js.Dynamic.literal(a = 1, b = 2)

@JSExportTopLevel("testObj2")
val testObj2 = new js.Object:
  val a = 1
  val b = 2

and compile it using Scala CLI version: 1.4.3:

scala-cli --power package --js -f object-literal.scala

The relevant generated js output (formatted using prettier):

function $c_Lobject$minusliteral$package$() {
	$n_Lobject$minusliteral$package$ = this;
	$t_Lobject$minusliteral$package$__testObj1 = $m_sjs_js_special_package$().ar(
		$m_sr_ScalaRunTime$().az(
			new ($d_T2.r().C)([new $c_T2("a", 1), new $c_T2("b", 2)])
		)
	);
	$t_Lobject$minusliteral$package$__testObj2 = (() => {
		var this$6 = {};
		this$6.a = 0;
		this$6.b = 0;
		this$6.a = 1;
		this$6.b = 2;
		return this$6;
	})();
}

I assume that using above scala-cli configuration, all available optimizations are applied to compiling and linking the code.
But no object literal was generated, as stated in the docs. Instead, a pretty complex expression is emitted.

Using an anonymous js.Object instance as in testObj2 comes closer to a native object definition.

Background: Seeing how easy it is to compile scala to js using scala-cli, I was hoping to develop a concise and lightweight DSL with scala.js on top of an audio processing library using a verbose js API, and to use this DSL alongside the rest of a js/ts codebase. But seeing that js.Dynamic.literal - which would be needed a lot - is emitting pretty complex code, i am worrying about too much overhead with such a solution.

@sjrd sjrd changed the title js.Dynamic.literal does not generate a Javascript object literal (using scala-cli to compile) js.Dynamic.literal does not generate a JS object literal with Scala 2.13/3. Sep 17, 2024
@sjrd sjrd added the optimization Optimization only. Does not affect semantics or correctness. label Sep 17, 2024
@sjrd sjrd self-assigned this Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optimization Optimization only. Does not affect semantics or correctness.
Projects
None yet
Development

No branches or pull requests

2 participants