Skip to content

Commit

Permalink
Merge pull request #50 from SciNim/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
Clonkk authored Mar 27, 2024
2 parents da5f633 + 32a2f82 commit e6bc797
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion nimjl.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Nimjl
# Licensed and distributed under MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
version = "0.8.0"
version = "0.8.2"
author = "Regis Caillaud"
description = "Nim Julia bridge"
license = "MIT"
Expand Down
44 changes: 35 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Mostly quality-of-life improvements, especially when handling arrays.

## Limitations

* Avoid using global scope for Julia function call. Always have everything inse proc / func. It's good practice anyway

* Value conversion Nim -> Julia are done **by copy**.
* Arrays are an exception to this rule and can be created from buffer / are accessible using a buffer.

Expand All @@ -63,24 +65,33 @@ Mostly quality-of-life improvements, especially when handling arrays.
* If you need Windows support, consider opening an issue or a PR :).
* Otherwise, just use WSL

# Examples
# Examples & tips

## Examples

Here is the basic example:
```nim
import nimjl
proc main() =
Julia.init() # Initialize Julia VM. Subsequent call will be ignored
var myval = 4.0'f64
# Call Julia function "sqrt" and convert the result to a float
var res = Julia.sqrt(myval).to(float64)
echo res # 2.0
Julia.init() # Initialize Julia VM. Subsequent call will be ignored
var myval = 4.0'f64
# Call Julia function "sqrt" and convert the result to a float
var res = Julia.sqrt(myval).to(float64)
echo res # 2.0
when isMainModule:
main()
```

JlVmExit() seems optionnal. It's present in the C API but not calling it doesn't seem to cause any problem.

Nonetheless, if you use OS resources from Julia it is probably better to call Julia.exit() / JlVmExit() for a clean exit.

## Setting up Julia dependency

It is now possible to embed Julia files inside a Nim compiled binary to easily distribute Julia code. To make distribution possible, an API to call ``Pkg.add("...")`` has also been added **with version number easy to specify**.
* It is now possible to embed Julia files inside a Nim compiled binary to easily distribute Julia code. To make distribution possible, an API to call ``Pkg.add("...")`` has also been added **with version number easy to specify**.

```nim
import nimjl
Expand All @@ -100,10 +111,25 @@ Julia.init:
# embed specific file; path should be relative to ``getProjectPath()``
file("localfile.jl")
```
See examples/ex09_embed_file.nim for a concrete example

Note that the order of the file matters.
See examples/ex09_embed_file.nim for a concrete example.

Take a look at ``tests/`` or ``examples/`` folder for typical examples.

* You can use Pkg: activate() to setup a virtual env
* Alternatively, you can embed a Julia file that setup your environment and dependencies and embed it **first**.
* Because files are evaluated in the order they are embedded, it will deterine the env for all the other files.

## Debugging

* Most error will come from incorrect type passed between Julia and Nim. Check function interface and return type first.

* If you have random segfault that are non-reproductible, that may be a cause of the Julia GC cleaning memory that Nim uses. Consider using jlGcRoot.

* If you do not work with fixed version package for Julia, you are at risk of code breaking when packages are updated / upgraded.


# License

This project is released under MIT License.

0 comments on commit e6bc797

Please sign in to comment.