Page 1 of 1

p-ascii and #null

Posted: Mon Jan 02, 2017 5:26 pm
by GPI

Code: Select all

PrototypeC.i __proto_luaL_loadfilex (lua_State.i, filename.p-ascii, mode.p-ascii)
Global luaL_loadfilex.__proto_luaL_loadfilex
it is not possible to call

Code: Select all

luaL_loadfilex(lua_State, "Hallo", #null)
Maybe it is a good idea to allow #null for p-ascii. At the moment it return a compiling error, because a string was expected. The lua.dll akzept for mode "b", "t", "bt" and 0 (0 = default = "bt").

Re: p-ascii and #null

Posted: Mon Jan 02, 2017 5:52 pm
by Mistrel
#Null is 0 while #Null$ is null-terminated empty string.

Strings in PureBasic are special as they hold additional internal state beyond a string of bytes, which is why they are handled differently from other languages.

For example, in C/C++, 0 can be considered a string because the byte-character of value '0' is null. In Java it also works with 'null' as a String is an object and null object references are valid.

PureBasic strings accept neither an integer 0 and has no concept of objects or null references, hence why you must provide explicit #Null or #Null$.

Re: p-ascii and #null

Posted: Mon Jan 02, 2017 6:35 pm
by Oma
GPI is right.
The same problem i have already posted here http://www.purebasic.fr/english/viewtop ... 23&t=64235 for ImportC on Linux.
The null-pointer-string #Null$ doesn't work with the pseudotypes 'p-ascii', 'p-utf8', ...
It seems that, in this case, the zero pointer is destroyed.

With an Import(C) you can declare the function twice, once with the 'AS' argument and the correct type for '0'.
But with an Prototype(C) no 'AS' is possible.

Regards, Charly

Re: p-ascii and #null

Posted: Mon Jan 02, 2017 7:41 pm
by GPI
Mistrel wrote:#Null is 0 while #Null$ is null-terminated empty string.
this is correct. The c-routine want here a "const char *mode" - #null would send a 0, #null$ would send a adress to a null-terminated empty string. The document says, that you should send a Null and not a null-terminated empty string.

Re: p-ascii and #null

Posted: Mon Jan 02, 2017 7:52 pm
by Mistrel
GPI wrote:The c-routine want here a "const char *mode" - #null would send a 0, #null$ would send a adress to a null-terminated empty string. The document says, that you should send a Null and not a null-terminated empty string.
You can't pass an integer as an argument to any form of PureBasic string. Altering this behavior would be a fundamental change; and in this light doesn't seem feasible.

Imo, this should have been asked in Coding Questions. You can still get the functionality you want:

Code: Select all

PrototypeC.i _null_proto_luaL_loadfilex (lua_State.i, filename.p-ascii, mode.i)
Global _null_luaL_loadfilex._null_proto_luaL_loadfilex
PrototypeC.i _string_proto_luaL_loadfilex (lua_State.i, filename.p-ascii, mode.p-ascii)
Global _string_luaL_loadfilex._string_proto_luaL_loadfilex

Procedure.i luaL_loadfilex(lua_State.i, filename.s, mode.s)
  If mode.s=""
    ProcedureReturn _null_luaL_loadfilex(lua_State, filename.s, #Null)
  EndIf
  
  ProcedureReturn _string_luaL_loadfilex(lua_State, filename.s, mode.s)
EndProcedure
The only thing you can't do with this is perform the original call with an empty string as #Null$ is being used as a conditional to pass #Null.

Does this help?

Re: p-ascii and #null

Posted: Tue Jan 03, 2017 7:04 am
by nco2k
Mistrel wrote:You can't pass an integer as an argument to any form of PureBasic string.
yes, but pseudotypes are meant to be used for lib/api calls.

the pb string gets converted to the required format and the pointer to the converted string gets passed to the function. i dont see a reason why we shouldnt be able to simply pass 0 and simply skip the conversion process.

c ya,
nco2k

Re: p-ascii and #null

Posted: Tue Jan 03, 2017 8:28 am
by Josh
Mistrel wrote:... while #Null$ is null-terminated empty string
No! You mix here two different things:

#Null$ is a null-string
#Empty$ would be a null-terminated empty string

Code: Select all

Procedure Test (a$, b$)
  
  Debug @a$
  Debug @b$
  
EndProcedure

Test (#Null$, #Empty$)

Re: p-ascii and #null

Posted: Tue Jan 03, 2017 11:16 am
by Oma
Hi, from Help - General rules:
#Empty$: represents an empty string (exactly the same as "")
#Null$ : represents an null string. This can be used for API
functions requiring a null pointer to a string, or to really free a string.