need help with wstring and PB dll

General FreeBASIC programming questions.
Post Reply
srvaldez
Posts: 3016
Joined: Sep 25, 2005 21:54

need help with wstring and PB dll

Post by srvaldez »

in reference to Lizard https://www.purebasic.fr/english/viewtopic.php?t=76279
Lizard - Script language for symbolic calculations, arbitrary large and precise numbers and more
I got it partially working but there are some problems
1: no matter what I tried, I get a warning of passing a different pointer on 2nd argument to Kernel_BindCallback, however at least the error function seems to work
2: why do I get a crash if at the end I deallocate the wstrings?
3: if I at the input prompt I enter "Calculate(Pi)" I get an error but if I set the input string to "Calculate(Pi)" in the code and then compile/run then no problem, also no problem if in the code I concatenate "Calculate("+input_+")"
here's my test code

Code: Select all

#inclib "Lizard"

Enum Event
	Event_Print = 1
	Event_Message
	Event_Error
End Enum

extern "Windows"
	declare function Kernel_BindCallback(byval as integer, byval CallbackAddress as sub ptr) as integer
'	Kernel_DuplicateExpression
	declare function Kernel_EvaluateExpression(byval p as any ptr) as any ptr
'	Kernel_FreeExpression
'	Kernel_GetValue
	declare function Kernel_Initialize(byval p as wstring ptr) as integer
	declare function Kernel_InputExpression(byval p as wstring ptr) as any ptr
	declare function Kernel_OutputExpression(byval p as any ptr) as wstring ptr
'	Kernel_Reset
'	Kernel_SetValue
	declare function Kernel_Terminate() as integer
	declare function Kernel_Version() as wstring ptr


sub Callback_Print(byval Text as wstring ptr)
	Print *Text
End sub

sub Callback_Message(byval Text as wstring ptr, byval SymbolName as wstring ptr, byval ErrorName as wstring ptr)
	Print "  ";*SymbolName;".";*ErrorName;"  ";
	Print *Text
End sub

sub Callback_Error(byval Text as wstring ptr)
	Print "  Syntax error: \";*Text
End sub

end extern

dim as double tm
dim as wstring ptr input_=callocate(1024), output_=callocate(1024*1024)
dim as any ptr pin, pout 'pin=allocate(1024*1024), pout=allocate(1024*1024)
if Kernel_Initialize(0) then
	Kernel_BindCallback(Event_Error, @Callback_Error())
	Kernel_BindCallback(Event_Message, @Callback_Message())
	Kernel_BindCallback(Event_Print, @Callback_Print())
else
	print "unable to initialize"
	sleep
	end
end if

*input_=" "
while *input_<>""
	input "-> ",*input_
	if *input_="" then exit while
	'*input_="Calculate("+(*input_)+", 50)"
	tm=timer
	pin=Kernel_InputExpression(input_)
	pout=Kernel_EvaluateExpression(pin)
	output_=Kernel_OutputExpression(pout)
	print *output_
	tm=timer-tm
	print tm
wend
'w=Kernel_Version() 'crash
'print *w
Kernel_Terminate()
'deallocate(w) 'crash
'deallocate(pin)
'deallocate(pout)
adeyblue
Posts: 194
Joined: Nov 07, 2019 20:08

Re: need help with wstring and PB dll

Post by adeyblue »

srvaldez wrote: May 23, 2022 2:49 I got it partially working but there are some problems
1: no matter what I tried, I get a warning of passing a different pointer on 2nd argument to Kernel_BindCallback, however at least the error function seems to work
As that function apparently takes multple different signatures of callback, you'll have to set it to Any Ptr. Those extra () at the end of @Callback_Error() (and the others) might then cause problems. They might not but either way they're not required.
2: why do I get a crash if at the end I deallocate the wstrings?
In that commented code you missed the *, changing w instead of *w - so it tried to free the pointer returned by the library rather than the one you allocated
srvaldez
Posts: 3016
Joined: Sep 25, 2005 21:54

Re: need help with wstring and PB dll

Post by srvaldez »

thank you, I had gone to bed and was reflecting on that and realized my mistake
srvaldez
Posts: 3016
Joined: Sep 25, 2005 21:54

Re: need help with wstring and PB dll

Post by srvaldez »

this compiles OK and it seems to function OK

Code: Select all

#inclib "Lizard"

Enum Event
	Event_Print = 1
	Event_Message
	Event_Error
End Enum

extern "Windows"
	declare function Kernel_BindCallback(byval as integer, byval CallbackAddress as any ptr) as integer
'	Kernel_DuplicateExpression
	declare function Kernel_EvaluateExpression(byval p as any ptr) as any ptr
'	Kernel_FreeExpression
'	Kernel_GetValue
	declare function Kernel_Initialize(byval p as wstring ptr) as integer
	declare function Kernel_InputExpression(byval p as wstring ptr) as any ptr
	declare function Kernel_OutputExpression(byval p as any ptr) as wstring ptr
'	Kernel_Reset
'	Kernel_SetValue
	declare function Kernel_Terminate() as integer
	declare function Kernel_Version() as wstring ptr


sub Callback_Print(byval Text as wstring ptr)
	Print *Text
End sub

sub Callback_Message(byval Text as wstring ptr, byval SymbolName as wstring ptr, byval ErrorName as wstring ptr)
	Print "  ";*SymbolName;".";*ErrorName;"  ";
	Print *Text
End sub

sub Callback_Error(byval Text as wstring ptr)
	Print "  Syntax error: \";*Text
End sub

end extern

dim as double tm
dim as wstring ptr input_=callocate(1024)
dim as wstring ptr output_'=callocate(1024)
dim as any ptr pin, pout 'pin=allocate(1024*1024), pout=allocate(1024*1024)
if Kernel_Initialize(0) then
	Kernel_BindCallback(Event_Error, @Callback_Error)
	Kernel_BindCallback(Event_Message, @Callback_Message)
	Kernel_BindCallback(Event_Print, @Callback_Print)
else
	print "unable to initialize"
	sleep
	end
end if

*input_=" "
while *input_<>""
	input "-> ",*input_
	if *input_="" then exit while
	*input_="Calculate("+(*input_)+", 100)"
	tm=timer
	pin=Kernel_InputExpression(input_)
	pout=Kernel_EvaluateExpression(pin)
	output_=>Kernel_OutputExpression(pout)
	print *output_
	tm=timer-tm
	print tm
wend
'output_=Kernel_Version() 'crash
'print *output_
Kernel_Terminate()
deallocate(input_)

'deallocate(pin)
'deallocate(pout)
btw, if you try this then you need to know that Lizard's commands are case sensitive
for example you can enter "Calculate(Pi, 50)"
one thing I am not sure about is the function Kernel_OutputExpression(byval p as any ptr) as wstring ptr
it seems to work but are there memory leaks/corruptions ? who's managing the memory for the wstring ?
how could I call the function in a safe and clean way ?
Post Reply