Skip to content

Commit

Permalink
added Dpdl example for Native interface
Browse files Browse the repository at this point in the history
  • Loading branch information
armincosta committed May 15, 2024
1 parent 6a1df5e commit fb1bb14
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 4 deletions.
124 changes: 124 additions & 0 deletions DpdlLibs/native/dpdlNativeExample.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# File: native/dpdlNativeExample.h
#
# Example: Dpdl sample code that loads a native library (libc) and calls some functions
#
# This code allocates memory via the native function 'malloc' , opens a file in read-write mode, write some values
# to the allocated buffer and writes the buffer to the file.
# The file is than re-opened in read only mode and the values are readed back and printed out.
#
# Author: A.Costa
# e-mail: ac@dpdl.io
#
#
import('native')

# main
println("Dpdl sample code that uses native function calls to 'libc'")

println("loading 'libc' library...")

object clib = native.loadLib("c")

raise(clib, "Error in loading library")

println("getting uid...")

int uid = clib.getuid()
println("uid: " + uid)

println("opening file in RW mode for writing...")

object fcntl = getClass("Fcntl")

int fh = clib.open("./Test/TestWriteNative.txt", fcntl.O_RDWR)

raise(fh, "Error in opening file")

println("writing values...")

int i = 3
double d = 999.5d
float f = 23.0
long l = 10000000L

char ch1 = 'X'
char ch2 = 'D'
char ch3 = 'P'
char ch4 = 'D'
char ch5 = 'L'
char ch6 = 'X'
char ch7 = '\0'

object size = loadObj("size_t")
size.setValue(4096L)

long size_l = size.longValue()

object memory = clib.malloc(4096)
memory.setMemory(0L, 4096L, 0x00)

println("memory: " + typeof(memory))

object bytebuf = memory.getByteBuffer(0L, size_l)

bytebuf.putInt(i)
bytebuf.putDouble(d)
bytebuf.putFloat(f)
bytebuf.putLong(l)

bytebuf.putChar(ch1)
bytebuf.putChar(ch2)
bytebuf.putChar(ch3)
bytebuf.putChar(ch4)
bytebuf.putChar(ch5)
bytebuf.putChar(ch6)
bytebuf.putChar(ch7)

object sz = clib.write(fh, memory, size)

println("nr of bytes written: " + sz)

clib.close(fh)

println("reading from file...")

fh = clib.open("./Test/TestWriteNative.txt", fcntl.O_RDONLY)

memory.setMemory(0L, 4096L, 0x00)

object szr = clib.read(fh, memory, size)

println("nr of bytes read: " + szr)

object bufread = memory.getByteBuffer(0L, size_l)

object ri = bufread.getInt()
object rd = bufread.getDouble()
object rf = bufread.getFloat()
object rl = bufread.getLong()

object rch1 = bufread.getChar()
object rch2 = bufread.getChar()
object rch3 = bufread.getChar()
object rch4 = bufread.getChar()
object rch5 = bufread.getChar()
object rch6 = bufread.getChar()

println("values from file: ")
println("")
println("ri: " + ri)
println("rd: " + rd)
println("rf: " + rf)
println("rl: " + rl)

println("rch1: " + rch1)
println("rch2: " + rch2)
println("rch3: " + rch3)
println("rch4: " + rch4)
println("rch5: " + rch5)
println("rch6: " + rch6)

clib.close(fh)

println("")
println("finished")
7 changes: 6 additions & 1 deletion Dpdl_Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ The Dpdl sample scripts and Dpdl sample applications published on this repositor

## Dpdl sample Apps

More ddvanced sample applications written with Dpdl are published in this repository:
More advanced sample applications written with Dpdl are published in this repository:

[Dpdl-sample-Apps](https://github.com/Dpdl-io/Dpdl-sample-Apps)

These examples are demonstrators but are full running applications.

## Dpdl examples

Expand Down Expand Up @@ -157,6 +158,10 @@ The code above automatically generated the following Dpdl code: [ai/dpdlAICodeSo

[dpdlPointers.h](https://github.com/Dpdl-io/DpdlEngine/blob/main/DpdlLibs/dpdlPointers.h)

* Example Dpdl code that writes and reads a data buffer to a file using the native library functions in 'libc'

[native/dpdlNativeExample.h](https://github.com/Dpdl-io/DpdlEngine/blob/main/DpdlLibs/native/dpdlNativeExample.h)

* Example Dpdl script embedding ANSI C code (ISO C99) that is dynamically compiled in memory at runtime (fast) and executed

[C/dpdlCcompileExample.h](https://github.com/Dpdl-io/DpdlEngine/blob/main/DpdlLibs/C/dpdlCcompileExample.h)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ A dedicated Dpdl language plug-in (**DAN**) allows via <ins>**generative AI code
These features make Dpdl a powerful development platform for rapid prototyping, in particular also due to the fact that software written with Dpdl will be enabled to access thousands of existing high-quality software libraries.
Dpdl is not intended to replace, but to enable integration of different technologies seamlessly to leverage fast prototyping and foster research and development.

**Dpdl is designed to:**
## Dpdl is designed to:

### * Develop your ideas faster
### * On multiple platforms
Expand Down
24 changes: 22 additions & 2 deletions doc/Dpdl_native_Interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,32 @@ developed by

Dpdl allows to access native library functions of any given loaded native library seamlessly, in the same way as ordinary dpdl functions.

This features makes Dpdl a complete and powerful programming language to develop also low level system components.
This features makes Dpdl a complete and powerful programming language to interact also with low level system components.

By loading 'c' (see example below), Dpdl automatically selects the appropriate library, on Linux/Unix/MAC 'libc' and on Windows 'msvcrt'.
**Dpdl library:**

**`native`**

```python
loadLib(string lib_name) return object lib
mapLib(string lib_name, object obj) return object lib
```

Example:

```python
import('native')

object clib = loadLib("c")

clib.printf("This message comes from native print function: %s %d", "Hello, 23)
```

By loading the library 'c', Dpdl automatically selects the appropriate library, on Linux/Unix/MAC 'libc' and on Windows 'msvcrt'.

On Windows systems it's possible also to access the system library 'kernel32' and it provides COM support.


Example Dpdl code accessing the system library 'libc' functions:
```python
import('native')
Expand Down

0 comments on commit fb1bb14

Please sign in to comment.