Jump to content




string colon operator problem


  • You cannot reply to this topic
7 replies to this topic

#1 moomoomoo3O9

  • Members
  • 82 posts
  • LocationPork roll land

Posted 30 June 2015 - 03:45 AM

So, I posted about this issue I was having a while ago, and I finally narrowed down the problem, but before I definitively call it a bug, I want to make sure this behavior is unintended.

If you have two files as so:
file1:
function string.blah(str) return str:upper() end

file2:
shell.run"file1"
print(string.blah("hi"))
print(("bye"):upper())
print(("bye"):blah())

When running file2, it will print HI, then BYE and then give an attempt to call nil error on line 4.
Is this a bug?

Edit: It gets no error in the emulator mimic, interestingly enough, but it errors in-game.

Edited by moomoomoo309, 30 June 2015 - 03:50 AM.


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 30 June 2015 - 05:20 AM

Certainly looks like a bug to me, but I don't see why you feel you need two files to demonstrate it.

#3 Quintuple Agent

  • Members
  • 107 posts
  • LocationThis page

Posted 30 June 2015 - 05:54 AM

Ok, well I found something strange to go along with this.
Using the code
Spoiler

Used it just to test everything that could mess up, When used in both ccemu and ingame it runs as expected the first time and returns
Spoiler

However, when ran another time after the computer has been restarted it returns
Spoiler

The strVar:upper is still being overwritten
When tested on a different computer without the script on it
string.upper works as it should however strVar:upper() returns "Sill messed up"
so it is being overwritten on the entire ComputerCraft global table
Edit: In 1 of my 6 test strVar:upper() was not overwritten on the other computer, however all other tests had the effect.
Edit2: So as I have tested it appears to work with only upper, I have also tested rep and lower and it does not affect the environment of other computers nor does it change strVar:rep() / strVar:lower() at all
Edit3: A video to prove I am not insane

Edited by Quintuple Agent, 30 June 2015 - 06:55 AM.


#4 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 30 June 2015 - 07:52 AM

The problem here is that the string metatable is shared between computer, because they are all running in the same LuaVM. When you first load the world the first computer to boot up ends up with the original "string" table, while all other computers get a copy of the string API. That's why you can change the functions the first time but not later.

Because of how CC is setup you'll have to leave the strings alone, so no overrides here, nor additions :(

#5 moomoomoo3O9

  • Members
  • 82 posts
  • LocationPork roll land

Posted 30 June 2015 - 03:17 PM

View PostQuintuple Agent, on 30 June 2015 - 05:54 AM, said:

Ok, well I found something strange to go along with this.
Using the code
Spoiler

Used it just to test everything that could mess up, When used in both ccemu and ingame it runs as expected the first time and returns
Spoiler

However, when ran another time after the computer has been restarted it returns
Spoiler

The strVar:upper is still being overwritten
When tested on a different computer without the script on it
string.upper works as it should however strVar:upper() returns "Sill messed up"
so it is being overwritten on the entire ComputerCraft global table
Edit: In 1 of my 6 test strVar:upper() was not overwritten on the other computer, however all other tests had the effect.
Edit2: So as I have tested it appears to work with only upper, I have also tested rep and lower and it does not affect the environment of other computers nor does it change strVar:rep() / strVar:lower() at all
Edit3: A video to prove I am not insane
What blows my mind is that "a.upper()" worked. That should never work in Lua! This is some very weird behavior...

Should I post a new thread over in bug reports about this? (Even with mk's response above, a.upper() is a behavior that just...shouldn't happen. ("string").upper() should error every time; strings aren't tables, and Quintuple didn't change the string metatable!)

Edited by moomoomoo309, 30 June 2015 - 03:22 PM.


#6 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 30 June 2015 - 04:43 PM

No, that shouldn't error. Please see the relevant section of the Lua Reference Manual.

#7 moomoomoo3O9

  • Members
  • 82 posts
  • LocationPork roll land

Posted 01 July 2015 - 02:41 AM

View PostLyqyd, on 30 June 2015 - 04:43 PM, said:

No, that shouldn't error. Please see the relevant section of the Lua Reference Manual.
I'd agree with you if it was ("exampleString"):upper(), but ("exampleString").upper() shouldn't work, from what I've seen. I've never seen the dot operator work with strings, but perhaps I just have more to learn. :P

To my knowledge, what should work is as follows:
("wot"):upper() (which is the same as string.upper("wot"))
but, what worked due to the bug was:
("wot").upper() (which is the same as ("wot")["upper"]() It's actually the same as string.upper(nil))


I don't see how ("wot") has an "upper" index.

Edited by moomoomoo309, 01 July 2015 - 02:58 AM.


#8 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 01 July 2015 - 12:03 PM

View Postmoomoomoo309, on 01 July 2015 - 02:41 AM, said:

I'd agree with you if it was ("exampleString"):upper(), but ("exampleString").upper() shouldn't work, from what I've seen. I've never seen the dot operator work with strings, but perhaps I just have more to learn. :P

To my knowledge, what should work is as follows:
("wot"):upper() (which is the same as string.upper("wot"))
but, what worked due to the bug was:
("wot").upper() (which is the same as ("wot")["upper"]() It's actually the same as string.upper(nil))


I don't see how ("wot") has an "upper" index.

You have to remember that strings are similar to tables because:

  • they can be indexed
  • you can access it's metatable

You can index strings however you want. For example, this works:

local str = "a string"
local er = "er"

print( str["upp" .. er](str) ) --> A STRING

local a = str[ {"this is a table"} ]

print( a ) --> nil






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users