Call a foreign-language function: Difference between revisions

Content added Content deleted
m (→‎{{header|Ruby}}: more legible quoting)
(GNU Forth 0.7.0)
Line 43: Line 43:
Hello World!
Hello World!
; No value</lang>
; No value</lang>

=={{header|Forth}}
{{works with|GNU Forth|0.7.0}}

Every version of GNU Forth has experimented with a different means to do C foreign function calls. The current implementation resolves various incompatibilities which had plagued earlier mechanisms by parsing C header files and using the host's native toolchain (i.e. gcc and ld) to generate thunks.

<lang forth>
c-library cstrings

\c #include <string.h>
c-function strdup strdup a -- a ( c-string -- duped string )
c-function strlen strlen a -- n ( c-string -- length )

end-c-library

\ convenience function (not used here)
: c-string ( addr u -- addr' )
tuck pad swap move pad + 0 swap c! pad ;

create test s" testing" mem, 0 c,

test strdup value duped

test .
test 7 type \ testing
cr
duped . \ different address
duped dup strlen type \ testing

duped free throw \ gforth ALLOCATE and FREE map directly to C's malloc() and free()
</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==