>i am still trying to modify the J6 file ~system/examples/dll/jdll.ijs so it 
>works on 64b architectures in j7 and j8, maybe generally. The results so far 
>are appended below. i do not have any standard (non-reversed) architectures to 
>work with, so there may be a problem outside of Intel too.

>i did a major punt on the extended type. It is maybe tripping up on the length 
>of data, ie in addition to the length of its rank. Works fine in j32.

>As Martin pointed out there may be a problem with alignment. i was able to get 
>it to produce an error for a large integer, but it was annealed when the 
>variable was changed, and changed back. So it can be hard to track down, which 
>is generally problematic.

>The goal is to make something which is callable from another interpreted 
>language (such as AutoHotKey in my case) without access to compiler directives.

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. 32 or 64 bit machines = = = = = = = = =
conv=.1+mFact=:1+IF64 NB. ghh

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. write memory, return pointer = = = = = = = = =
mwrite=: 3 : 0
p=. mema l=. #y
y memw p,0,l
p
)

jclear=: 3 : 0 NB. = = = = = = = = =
'j.dll JClear x x' cd pJ
)

jcmd=: 3 : 0 NB. = = = = = = = = =
jdo 'JDAT=: ',y
jget 'JDAT'
)

jdo=: 3 : 0 NB. = = = = = = = = =
'j.dll JDo x x *c' cd pJ, boxopen y
)

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
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. t=32 do. p$ jgetobj &.> i2j memr d,len*4*mFact NB. ghh *mFact
elseif. (IF64 < t=64) do. p$ jgetext &> i2j memr d,len*4
elseif. 1 do. 'unrecognized datatype';<t
end.
)

NB. jfree = = = = = = = = =
jfree=: 3 : 0
'j.dll JFree x x' cd pJ
)

NB. jget = = = = = = = = =
NB. form: jget 'name'
NB. return value of name
jget=: 3 : 0
cmd=. 'j.dll JGetM x x *c *x *x *x *x'
'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. jgetext = = = = = = = = =
NB. form: jgetext address
jgetext=: 3 : 0
len=. i2j memr y,28 4 *mFact NB. ghh *mFact
10000 #. x: |. i2j memr y,32,(4*len  *(*:*:mFact)) NB. ghh *mFact^4 mr res
NB. (mFact^4) -: *:*:mFact, ...but first fails ...
)

NB. jinit = = = = = = = = =
jinit=: 'j.dll JInit x' & cd

NB. jset = = = = = = = = =
NB. form: 'name' jset data
NB. assign value to name
jset=: 4 : 0
rep=. 3!:1 y
NB. Seems to include intel64...
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 x x *c *x *x *x *x' cd pJ,dat
memf ps
memf pv
if. e do.
  wdinfo 'error code: ',":e
end.
empty''
)

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''
)

dllexamples=: 0 : 0 NB. = = = = = = = = =
jdo 'ABC=: 3 4 $''abcdef'''
jdo 'ABC=: i.3 4'
jdo 'ABC=: i.2 3 4'   NB. Difference in padding twixt even & odd ranks?
jdo 'ABC=: 9e9*i.3 4' NB. Floats
jdo 'ABC=: 1.1+i.3 4'
jdo 'ABC=: 1j1+i.3 4' NB. complex
jdo 'ABC=: <"0[i.3 4' NB. Boxing cells
jdo 'ABC=: <"1[i.3 4' NB. Boxing lines
jdo 'ABC=: <"_[i.3 4' NB. Boxing all
jdo 'ABC=: ''a'';<<"1[i.3 4' NB. Boxing various
jdo 'ABC=: i. (999999$1)$ 4' NB. high rank odd
jdo 'ABC=: i. (999998$1)$ 4' NB. high rank even
jdo 'ABC=: i. (99$1)$ 4x' NB. high rank of extended
jdo 'ABC=: i. 4x'
jdo 'ABC=: 99^99x'

jget 'ABC'
'ABC' jset i.2 3 4
'ABC' jset i.2 3
'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

Reply via email to