[Ur] interfacing with C or other languages - example system

Marc Weber marco-oweber at gmx.de
Sat Nov 27 17:16:49 EST 2010


Excerpts from Adam Chlipala's message of Sat Nov 27 18:43:20 +0100 2010:
> Marc Weber wrote:
> > Passing primitive datatypes (strings, ints, blobs, ...) from C to urweb
> > is known. Is there a way to pass more complex (foreign types) such as
> > XML documents you can run XPATH queries on etc?
> 
> XML isn't a particularly good example, since Ur/Web values of [xml] 
> types are represented as strings at runtime, which makes them trivial to 
> pass back and forth with C or JavaScript.  You could easily do the same 
> for some other presentation of XML.
> 
> For structured types that should be FFI-compatible, I recommend 
> declaring an abstract type with a set of constructor and modification 
> functions, implemented in the foreign language.

First use case:

I'd like to teach urweb to run arbitrary system commands.

However there are two important return types:
a) the stdout data (blob). Is there already a blob_to_str function? I
    didn't find it
b) exit code.

So how do I pass those two values back to ur?

Should I creat a struct for that holding both?
How to pass the pointer to the struct to Ur? As uw_Basis_int ?
This will work but would be causing some headache to me.

Should I be doing kind of bookkeeping?

  job_id:Int <- system("cat file.txt", bufsize, timeout)
  blob_of_job <- system_blob(job_id)
  status <- system_status(job_id)
  stoppedBy <- systemt_stopped_by(job_id) // one of timeout, bufsize exceeded, or exited with exit code

Then I have my blob and status and reason why the function returned.
However because job_id is an int I could be passing bad data which
indicates that the interface is less than optimal. (Its a library which
can be wrapped ... so ... it would be ok.

Is this the way to go?

Thne you have to do bookkeeping on the C side in order to serve thoese
system_(exitstatus, stoppedBy, blob) functions

The perfect ur interface would look like this:

system: (cmd:string) (timeout:int) returning one of
  | Ok status blob
  | Timeout blob

Marc Weber



More information about the Ur mailing list