how to add a primitive procedure?

2 views
Skip to first unread message

marcomaggi

unread,
Oct 23, 2010, 2:36:59 AM10/23/10
to Mosh Developer Disscus
Suppose I write a C++ function implementing a new primitive: how do I
register it so that Mosh exports it at the Scheme level from, for
example, the system library?
--
Marco Maggi

Yuki Okumura

unread,
Oct 23, 2010, 3:28:53 AM10/23/10
to Mosh Developer Disscus
You can include yours into (n)mosh like these:

0) implement your C++ function in main.cpp or another place

1) wrap your function in mosh CProcedure interface

For example, see src/FFIProcedures.cpp internalFfiFreeEx which wraps
stdc free() function:

Object scheme::internalFfiFreeEx(VM* theVM, int argc, const Object*
argv)
{
DeclareProcedureName("free");
checkArgumentLength(1);
argumentAsPointer(0, p); // check if first argument is FFI pointer
and bind it to variable p
free((void*)p->pointer()); // actual function call.
return Object::Undef;
}

You can use argument checking macro declared in src/ProcedureMacro.h .

2) Get CProcedure object from your function and register it to mosh VM

Pass your function to Object::makeCProcedure() and register it
using setValueString(). You should do that in main.cpp, just before
activateR6RSMode()

see src/main.cpp :
theVM->setValueString(UC("%get-stack-trace-
obj"),Object::makeCProcedure(internalGetStackTraceObj));

this registers C++function internalGetStackTraceObj as scheme
procedure %get-stack-trace-obj.

3) Call it from scheme

Use symbol-value procedure from (mosh) library.

(import (mosh)) ;; import symbol-value from (mosh)
(define %get-stack-trace-obj (symbol-value '%get-stack-trace-obj))

(%get-stack-trace-obj)

Note that larceny-like (import (primitives %get-stack-trace-obj)) form
is not available on
psyntax based mosh.


These are totally undocumented...
Reply all
Reply to author
Forward
0 new messages