i modified,
J6 examples/dll/jdll.ijs
>as below, so it works in 64b J. However i did major punts on boxed and
>extended types.
greg
~krsnadas.org
---
NB. JDLL example
NB.
NB. This script gives examples of calling the J DLL from J. Similar
NB. routines may be written in other languages that can call DLLs.
NB.
NB. The internal representation used is that for 32-bit intel machines.
NB.
NB. main definitions:
NB. jclear clear J session
NB. jcmd send sentence, return result
NB. jdo send sentence to be executed
NB. jget retrieve value of J noun
NB. jset assign value to J noun
NB.
NB. utilities:
NB. jinit initialize J instance
NB. jfree free J instance
NB.
NB. Loading the script initializes a J instance, defining
NB. global pJ as the handle. Use jfree to free it.
NB.
NB. see examples at the end of the script, and the dictionary
NB. entry for the 3!: foreign conjunction.
require 'dll'
NB. = = = = = = = = = ===
NB. 32 or 64 bit machines
conv=.1+mFact=:1+IFBIG=:9e9<9!:20'' NB. ghh 130616
i2j=: _2 & ic NB. binary integer to J
f2j=: _2 & fc NB. binary float to J
j2i=: 2 & ic NB. J to binary integer
j2f=: 2 & fc NB. J to binary float
NB. 64 bit machines
i2j=: (-conv) & ic NB. binary integer to J
f2j=: _2 & fc NB. binary float to J
j2i=: conv & ic NB. J to binary integer
j2f=: 2 & fc NB. J to binary float
NB. = = = = = = = = = ===
NB. write memory, return pointer
mwrite=: 3 : 0
p=. mema l=. #y
y memw p,0,l
p
)
NB. = = = = = = = = = ===
jclear=: 3 : 0
'j.dll JClear i i' cd pJ
)
NB. = = = = = = = = = ===
jcmd=: 3 : 0
jdo 'JDAT=: ',y
jget 'JDAT'
)
NB. = = = = = = = = = ===
jdo=: 3 : 0
'j.dll JDo i i *c' cd pJ, boxopen y
)
NB. = = = = = = = = = ===
NB. jfix
NB. form: trs jfix data_address
NB. trs=type,rank,shape_address
jfix=: 4 : 0
't r s'=. x
d=. y,0
len=. */p=. i2j memr s,0,r*4*mFact NB. ghh *mFact
assert. p<99999 NB. ghh 130617
if. t=1 do. p$ (1{a.)=memr d,len
elseif. t=2 do. p$ memr d,len
elseif. t=4 do. p$ i2j memr d,len*4*mFact NB. ghh *mFact
elseif. t=8 do. p$ f2j memr d,len*8
elseif. t=16 do. p$ _2 j./\ f2j memr d,len*16*mFact NB. ghh *mFact
elseif. (2-mFact)* t=32 do. p$ jgetobj &.> i2j memr d,len*4 *mFact
elseif. (2-mFact)* t=64 do. p$ jgetext &> i2j memr d,len*4
elseif. 1 do. 'unrecognized datatype'
end.
)
NB. = = = = = = = = = ===
NB. jfree
jfree=: 3 : 0
'j.dll JFree i i' cd pJ
)
NB. = = = = = = = = = ===
NB. jget
NB. form: jget 'name'
NB. return value of name
jget=: 3 : 0
cmd=. 'j.dll JGetM i i *c *i *i *i *i'
'e p n t r s d'=. cmd cd pJ,(,y);4#<,0
if. e do.
wdinfo 'error code: ',":e
return.
end.
(t,r,s) jfix d
)
NB. = = = = = = = = = ===
NB. jgetobj
NB. form: jgetobj address
jgetobj=: 3 : 0
'p j j t c l r'=. i2j memr y,0 28*mFact NB. ghh *mFact
(t,r,y+28) jfix y+p
)
NB. = = = = = = = = = ===
NB. jgetext
NB. form: jgetext address
jgetext=: 3 : 0
len=. i2j memr y,28 4 *mFact
10000 #. x: |. i2j memr y,32,4*len
)
NB. = = = = = = = = = ===
NB. jinit
jinit=: 'j.dll JInit i' & cd
NB. = = = = = = = = = ===
NB. jset
NB. form: 'name' jset data
NB. assign value to name
jset=: 4 : 0
rep=. 3!:1 y
NB. assert. (225{a.) = {.rep NB. intel32
type=. 3!:0 y
if. type > 16 do.
'boxed or other types > 16 not supported by jset' return.
end.
rank=. #$y
shape=. (16 + i. rank*4){rep
val=. (16 + rank*4)}.rep
ps=. mwrite shape
pv=. mwrite val
dat=. x;,each type;rank;ps;pv
e=. 0 pick 'j.dll JSetM i i *c *i *i *i *i' cd pJ,dat
memf ps
memf pv
if. e do.
wdinfo 'error code: ',":e
end.
empty''
)
NB. = = = = = = = = = ===
NB. check DLL will work
3 : 0 ''
NB. if. -. IFWIN32 do. j=. 'This demo works only under 32-bit Windows.'
NB. wdinfo 'JDLL';j
NB. return.
NB. end.
dll=. tolower each {."1 [ 1!:0 <'*.dll'
if. -. (<'j.dll') e. dll do.
wdinfo 'JDLL';'Could not find J.DLL'
end.
pJ=: jinit''
)
NB. = = = = = = = = = ===
dllexamples=: 0 : 0
jdo 'ABC=: 3 4 $''abcdef'''
jdo 'ABC=: i.3 4'
jdo 'ABC=: 1.1+i.3 4'
jdo 'ABC=: 1j1+i.3 4'
jdo 'ABC=: <"0[i.3 4'
jdo 'ABC=: <"1[i.3 4'
jdo 'ABC=: <"_[i.3 4'
jdo 'ABC=: ''a'';<<"1[i.3 4'
jdo 'ABC=: i.3 4x'
jget 'ABC'
'ABC' jset i.2 3 4
jcmd 'i.3 4'
jcmd '<"0 i.3 4'
jclear''
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm