Skip to content

Instantly share code, notes, and snippets.

@ashgti
Created May 21, 2010 22:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ashgti/409521 to your computer and use it in GitHub Desktop.
.sub 'main' :main
$P0 = 'c_int16'()
.local pmc lib
lib = loadlib 'testing_lib'
$P1 = 'dlfunc'(lib, 'say', $P0, $P0)
$P2 = new 'c_int16'
$P3 = $P1($P2)
say $P3
$P4 = subclass 'Structure', 'sockaddr_in' # defining my own C data type structure
$P4['foo'] = $P0
$P5 = new $P4 # $P5 now has a new structure instance
$P6 = subclass 'c_int16', 'my_type' # Creating an alias of a type, data abstraction ftw
$P7 = new $P6 # Instansiating my new data type
$P7 = 1234 # works fine
$P7 = 32768 # failure because of the limit on a signed 16 bit int
$P1 = 'dlfunc'(lib, 'foo', $P0, $P5) # takes either an instance type
$P1 = 'dlfunc'(lib, 'foo', $P0, $P4) # or a class name for definition
# An example of a callback from C to PIR
lib = loadlib 'libc'
.local pmc qsort
qsort = lib['qsort'] # why shouldn't this work?
$P1 = 'array'($P0, 5)
$P2 = new $P1
$P2[0] = 99
$P2[1] = 5
$P2[2] = 1
$P2[3] = 7
$P2[4] = 33
$P3 = ptr($P0)
.local pmc cmpfunc_sig, cmpfunc
cmpfunc_sig = 'cfunctype'($P0, $P3, $P3)
cmpfunc = cmpfunc_sig("my_cmp_function")
$P4 = sizeof($P0)
qsort($P2, 5, $P4, cmpfunc)
.end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment