[Java] Kinx Tips-Create an executable file

4 minute read

Kinx Tips-Create an executable file

Introduction

The script language Kinxdeliveredby”“ItlookslikeJavaScript,thebrain(contents)isRuby,(stabilityisAC/DC)”). This time it is Tips.

What do you guys do when you create an executable? …Yeah, you usually write it in C or C++ and compile it. I hope it will be easier to make.

Kinx doesn’t support creating an independent exe, but in the world of Kinx it makes an exe so that you can create a module that can be executed by pretending to be an independent execution command.

A little around that.

Command kxrepl.exe, kxtest.exe

Actually, from the v0.13.1 release, the commands kxrepl.exe and kxtest.exe are included. In Linux version, .exe is not included, but kxrepl and kxtest.

Let’s run kxrepl.

$ kxrepl
kinx[ 0]> .quit

REPL is working.

Next is kxtest.

$ kxtest -v -T declaration.md
Test Cout = 11
Entry: doc/spec/statement/declaration.md
    Suite: Declaration statement
        Case[ 0]: Normal case ................................... successful (0.10s)
        Case[ 1]: With initializer .............................. successful (0.09s)
        Case[ 2]: With initializer of expression ...... successful (0.09s)
        Case[ 3]: Multiple variable declaration ................. successful (0.10s)
        Case[ 4]: Constant value (1) ...................................... successful (0.07s)
        Case[ 5]: Constant value (2) ...................................... successful (0.10s)
        Case[ 6]: Constant value (3) ...................................... successful (0.07s)
        Case[ 7]: Constant value (4) ...................................... successful (0.07s)
        Case[ 8]: Constant value (5) ...................................... successful (0.09s)
        Case[ 9]: Destructuring assignment (1) .................. successful (0.09s)
        Case[10]: Destructuring assignment (2) .................. successful (0.07s)


<Test Result>
    Total Test Cases: 11
        Successful :11
        Failed: 0
        Warning: 0

SpecTest is working.

Let’s try another one.

$ diff -s kxrepl kxtest
Files kxrepl and kxtest are identical

I get the message “Files are identical”. Or rather, it is. The two are exactly the same as binaries.

--exec option talk

Before I talk about this, I’ll write one piece of information about Kinx options. The option is --exec. As mentioned in the README, this option supports two options:

  • --exec:repl … Execute REPL.
  • Execute --exec:specttest … SpecTest.

This mechanism is as follows.

  1. After recognizing --exec:xxx, look for lib/exec/xxx.kx or lib/exec/3rdparty/xxx.kx in the folder containing the Kinx executable file.
  2. If the file exists, assume that the file is specified as a script file and execute it.

So REPL and SpecTest work by looking for the lib/exec/repl.kx and lib/exec/spectest.kx files and executing them, respectively. This mechanism also allows REPL and SpecTest modifications to be made without modifying the binary.

kxrepl

By the way, kxrepl binary (= kxtest binary), but what kind of structure is it? The answer is this.

  1. Get your own executable file name (let’s call it name) (you can get it from argv[0]).
  2. Update the argument list by inserting --exec:name at the beginning of the command line argument.
  3. Pass control to the main Kinx logic.

How about that? It will automatically refer to lib/exec/name.kx (or lib/exec/3rdparty/name.kx) and execute it!

So, kxrepl will automatically execute lib/exec/kxrepl.kx and kxtest will automatically execute lib/exec/kxtest.kx.

Hmm? , Is there such a file? Yes, I added it (= there is). Let’s look inside.

lib/exec/kxrepl.kx


using exec.repl;

lib/exec/kxtest.kx


using exec.spectest;

Only this. It was realized by preparing a file that only using in. It will find repl.kx and spectest.kx according to the using search path, so this works correctly. So, if you change kxrepl.exe to repl.exe, it will work as well.

So you already understand.

Let’s make the original exe

Here’s how to create an original exe file. The Kinx dll and various libraries are required for operation, so you must put the created exe in the same location as kinx.exe, but you can do what you want with a single command. I will.

With a concrete example.

kxcat.exe

Let’s create a kxcat command as something like a cat command. Outputs the specified files in the specified order. Use kxcat so that the name does not overlap with the cat command. For the time being, there are no options and only multiple files are accepted.

lib/exec/3rdparty/kxcat.kx


$$.each {
    // Ignoring the script file name.
    if (_2> 0) {
        System.print(File.load(_1));
    }
};

Now, copy kxtest.exe and rename it. The commands are different on Windows and Linux, but what you want to do is the same.

Windows


$ copy /y kxtest.exe kxcat.exe

Linux


$ cp -f kxtest kxcat

After copying, execute kxcat command as it is!

$ ./kxcat README.md ChangeLog.md
<p align="right">
    <img src="https://github.com/Kray-G/kinx/workflows/Unit%20Test/badge.svg?branch=master"/>
    <img src="http://img.shields.io/badge/license-MIT-blue.svg?style=flat"/>
</p>

...(abridgement)

## V0.1.0 (1st Preview Release)

* Initial Release.

You’re done.

If you want to run it as installed, on Linux there is a binary for the kinx command in the following location, so for example in the above case put the kxcat command in the same location. kxrepl and kxtest are also placed in the same place.

$ which kinx kxrepl kxtest
/usr/bin/kinx
/usr/bin/kxrepl
/usr/bin/kxtest

in conclusion

It is okay to write it in C and compile it to make an executable file, but I want to make what I wrote in a script into an executable file. However, if you include all necessary libraries and dlls, the size will be quite good, so I tried to divide it here so that Kinx itself can be commanded on the assumption that it exists.

I think this is convenient.Also, as soon as you do it, if you can specify the location of kinx.dll with an environment variable or pass it as an option, the .exe file itself will improve everywhere, so that correspondence will be in the future May be done (if requested). Like running xxx.kx in the same location as xxx.exe. Is that more convenient? The location of the dll needs to be specified somehow.

See you next time.