<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: peter molnar</title>
    <description>The latest articles on DEV Community by peter molnar (@pm3).</description>
    <link>https://dev.to/pm3</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3093934%2Fc2772879-7a49-45cb-a505-e247bafcbd86.png</url>
      <title>DEV Community: peter molnar</title>
      <link>https://dev.to/pm3</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pm3"/>
    <language>en</language>
    <item>
      <title>java-js: A Lightweight JavaScript Subset for the JVM</title>
      <dc:creator>peter molnar</dc:creator>
      <pubDate>Sat, 26 Apr 2025 16:25:35 +0000</pubDate>
      <link>https://dev.to/pm3/java-js-a-lightweight-javascript-subset-for-the-jvm-7bd</link>
      <guid>https://dev.to/pm3/java-js-a-lightweight-javascript-subset-for-the-jvm-7bd</guid>
      <description>&lt;h1&gt;
  
  
  Introducing java-js: JavaScript Subset for the JVM
&lt;/h1&gt;

&lt;p&gt;Have you ever wished for the flexibility of JavaScript syntax with the performance and reliability of the JVM? That's exactly what &lt;strong&gt;java-js&lt;/strong&gt; delivers. I'm excited to share this project that implements a carefully curated subset of JavaScript features designed specifically for Java environments.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is java-js?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;java-js&lt;/strong&gt; is a minimalist JavaScript interpreter that runs natively on the JVM. It provides syntax compatibility with a selected subset of JavaScript while using native Java objects under the hood. This means code written for java-js can be executed in standard JavaScript engines, but not necessarily vice versa.&lt;/p&gt;

&lt;p&gt;One of the key differences is that java-js eliminates many of JavaScript's quirky behaviors (the "WTF moments" that often confuse developers) in favor of more predictable operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Power of AI Collaboration
&lt;/h2&gt;

&lt;p&gt;An interesting aspect of this project is that it was developed in collaboration with Claude 3.7, Anthropic's language model. Claude contributed significantly to the implementation of core components:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Component&lt;/th&gt;
&lt;th&gt;Claude 3.7&lt;/th&gt;
&lt;th&gt;My Contribution&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Lexer&lt;/td&gt;
&lt;td&gt;90%&lt;/td&gt;
&lt;td&gt;10%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Parser&lt;/td&gt;
&lt;td&gt;80%&lt;/td&gt;
&lt;td&gt;20%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AstNodes&lt;/td&gt;
&lt;td&gt;70%&lt;/td&gt;
&lt;td&gt;30%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Types &amp;amp; Ops&lt;/td&gt;
&lt;td&gt;70%&lt;/td&gt;
&lt;td&gt;30%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SDK Functions&lt;/td&gt;
&lt;td&gt;80%&lt;/td&gt;
&lt;td&gt;20%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tests&lt;/td&gt;
&lt;td&gt;100%&lt;/td&gt;
&lt;td&gt;0%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Documentation&lt;/td&gt;
&lt;td&gt;90%&lt;/td&gt;
&lt;td&gt;10%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This collaboration demonstrates how AI can accelerate development by handling implementation details while I focused on architectural decisions and integration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Features
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Supported Types
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;All the basics: &lt;code&gt;null&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt;, and &lt;code&gt;number&lt;/code&gt; (as &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;long&lt;/code&gt;, and &lt;code&gt;double&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Collections as &lt;code&gt;array&lt;/code&gt; (Java &lt;code&gt;List&lt;/code&gt;) and &lt;code&gt;object&lt;/code&gt; (Java &lt;code&gt;Map&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;First-class functions using custom &lt;code&gt;JsFunction&lt;/code&gt; or native &lt;code&gt;BiFunction&amp;lt;Scope, List&amp;lt;Object&amp;gt;, Object&amp;gt;&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Error handling&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  JavaScript Syntax Support
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Variable declarations with &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Standard operators including assignments and arithmetic&lt;/li&gt;
&lt;li&gt;Control flow with conditionals and loops&lt;/li&gt;
&lt;li&gt;Modern JavaScript features like:

&lt;ul&gt;
&lt;li&gt;Arrow functions&lt;/li&gt;
&lt;li&gt;Template literals&lt;/li&gt;
&lt;li&gt;Optional chaining&lt;/li&gt;
&lt;li&gt;Null coalescing operator&lt;/li&gt;
&lt;li&gt;Partial Array and Object destructing&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Compile Once, Run Many
&lt;/h3&gt;

&lt;p&gt;One of the project's strengths is its thread-safe design. Once a script is compiled to an AST, it can be executed multiple times across different threads without risk of shared state corruption:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;JsLexer&lt;/span&gt; &lt;span class="n"&gt;lexer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;JsLexer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;JsParser&lt;/span&gt; &lt;span class="n"&gt;parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;JsParser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lexer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;tokenize&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;span class="nc"&gt;ASTNode&lt;/span&gt; &lt;span class="n"&gt;programNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parser&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parse&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Run in parallel or sequentially as needed&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++){&lt;/span&gt;
     &lt;span class="nc"&gt;Scope&lt;/span&gt; &lt;span class="n"&gt;rootScope&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;JsSdk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createRootScope&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
     &lt;span class="n"&gt;programNode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;exec&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rootScope&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Use Cases
&lt;/h2&gt;

&lt;p&gt;java-js is perfect for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Embedding a scripting engine in Java applications&lt;/li&gt;
&lt;li&gt;Creating domain-specific languages with JavaScript-like syntax&lt;/li&gt;
&lt;li&gt;Configuration scripts that need conditional logic&lt;/li&gt;
&lt;li&gt;Providing a safe execution environment for user-defined logic&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  An Example
&lt;/h2&gt;

&lt;p&gt;Here's how simple it is to use java-js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;eu.aston.javajs.*&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;eu.aston.javajs.AstNodes.ASTNode&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;script&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"""           
                var a = 5;
                let b = 10;
                const sum = (x, y) =&amp;gt; x + y;

                var result = sum(a, b);
                """&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;Scope&lt;/span&gt; &lt;span class="n"&gt;rootScope&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;JsSdk&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createRootScope&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="nc"&gt;JsLexer&lt;/span&gt; &lt;span class="n"&gt;lexer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;JsLexer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;JsParser&lt;/span&gt; &lt;span class="n"&gt;parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;JsParser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lexer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;tokenize&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;span class="nc"&gt;ASTNode&lt;/span&gt; &lt;span class="n"&gt;programNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parser&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parse&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;programNode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;exec&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rootScope&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rootScope&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getVar&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"result"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Try It Out
&lt;/h2&gt;

&lt;p&gt;java-js is available as a Maven dependency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;eu.aston&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;java-js&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.0.0&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The complete source code is available on GitHub: &lt;a href="https://github.com/pm3/java-js" rel="noopener noreferrer"&gt;https://github.com/pm3/java-js&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🚫 Not Supported:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;new&lt;/code&gt; operator&lt;/li&gt;
&lt;li&gt;&lt;code&gt;async/await&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;class&lt;/code&gt;, &lt;code&gt;instanceof&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;import/export&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;fetch&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;regex&lt;/code&gt; - no regex object, only &lt;code&gt;String&lt;/code&gt; parameter in &lt;code&gt;String.match&lt;/code&gt; or &lt;code&gt;String.matchAll&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;some JavaScript WTF behaviors (like &lt;code&gt;[] == 0&lt;/code&gt;, etc.)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;java-js brings the simplicity and expressiveness of JavaScript syntax to the JVM while avoiding many of JavaScript's pitfalls. Whether you need a lightweight scripting solution for your Java application or just want to explore language implementation, I invite you to check out the project.&lt;/p&gt;

&lt;p&gt;I'd love to hear your thoughts and feedback! What features would you like to see added in future versions? &lt;/p&gt;

</description>
      <category>java</category>
      <category>javascript</category>
      <category>jvm</category>
      <category>interpreter</category>
    </item>
  </channel>
</rss>
