Giter VIP home page Giter VIP logo

cna's Introduction

Caché Native Access (CNA)

CNA provides an interface for using native C-compatible shared libraries without anything but Caché ObjectScript code. CNA is a wrapper for libffi. CNA consists of native library (libcna) and Caché class (CNA.CNA).

Installation

Linux

  • Build libfii: make libffi. If 'make' says that GLOBALS_HOME is undefined, then define it:
 export GLOBALS_HOME='path to Caché root directory'
  • Build libcna: make
  • Done! File we are looking for — libcna.so. After all you need to import "cna.xml" in any Caché namespace:
 do $system.OBJ.Load("path to cna.xml", "c")

Windows

For Windows-based systems you can try to do the same as for Linux (for building you may use mingw32 or mingw32-w64). Or you can just download binary files. Note: types (32-bit or 64-bit) of libcna, current Caché installation and external libraries must be equal. For building you can use MSYS2. How to:

  1. Install MSYS,
  2. Then run correct bash (32 or 64)
  3. Install make, textinfo with pacman -S make, textinfo
    • For 64bit install mingw-w64-x86_64-gcc with pacman -S mingw-w64-x86_64-gcc
    • For 32bit install gcc with pacman -S gcc
  4. Modify makefile, replace: cd libs/libffi && ./configure --build=$(BUILDSYS) --enable-shared=no && $(MAKE) with cd libs/libffi && ./configure --prefix=/mingw --build=$(BUILDSYS) --enable-shared=no && $(MAKE)
  5. Follow linux installation steps

Runnning tests

If you want to check if CNA works correctly on your system you may run the unit-tests. You will need native library with test functions 'libtest'. If you build CNA by yourself, it is already in the right place. If no — it must be placed in directory cna/tests/. Than you should set ^UnitTestRoot global to the right value and run the tests.

 set ^UnitTestRoot="path to 'cna/tests/' directory"  
 do ##class(%UnitTest.Manager).RunTest()

Simple example

Let's try to call strlen function from C standard library. In Windows it should be located in C:\Windows\System32\msvcrt.dll. In Linux — /usr/lib/libc.so. strlen returns the length of the string:

 size_t strlen ( const char * str );

Let's see what you need to do to call it from Caché:

 set cna = ##class(CNA.CNA).%New("path to libcna"); Creates object of CNA.CNA class.
 do  cna.LoadLibrary("path to libc")              ; Loads C standard library in CNA

 set string = cna.ConvertStringToPointer("Hello") ; Converts the string into char array, and saves pointer to the first element
                                                  ; That's how string are stored in C

 set argTypes = $lb(cna.#POINTER)                 ; Creates list of CNA.CNA pararmeters. Each parameter stands for the type of the function argument

 set result = cna.CallFunction("strlen",          ; Passes the name of the function,
                                cna.#SIZET,       ; type of return value,
                                argTypes,         ; list of argument types,
                                string)           ; and comma-separated arguments 

 write result, !                                  ; Writes the result (it should be 5)
 do cna.FreeLibrary()                             ; Frees the library

cna's People

Contributors

daugeldauge avatar eduard93 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cna's Issues

Automagical external function header definition

At the moment we describe arguument types via $lb() call, i.e.

set argTypes = $lb(..gd.#POINTER, ..gd.#POINTER, ..gd.#INT, ..gd.#INT)
do ..gd.CallFunction("gdImageGifAnimBegin", ..gd.#VOID, argTypes, im, out, 1, 0)

It would be cool if we would have some compile-time way to define function argument types, so type coercion will be done authomagically at the call moment.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.