Problem storing Lua code in dynamically created class side method.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem storing Lua code in dynamically created class side method.

Squeak - Dev mailing list
Hi all.

I am attempting to store the Wikimedia Lua Modules as Classes in my image. The idea being that when the Parser encounters a Module #invoke I can hand it off to the appropriate class and have it invoke Lua and return content to me.

I have extracted the Wikimedia Modules to a postgres table named 'modules'  and I am hitting that table and iterating over it to dynamically create the classes I need.
The code is a modified version of what I used to scrape the Squeak Wiki.


scrapeModulesToNewClass
|rows |
      rows := ((WikitextParserConnectionPool default executeQuery: 'select page_id, page_title, old_text from modules limit 2') at:1) rows.

rows do: [:each | |pageid pagetitle oldtext lua  methodTemplatePageId methodTemplatePageTitle methodTemplateLua methodSourcePageId methodSourcePageTitle methodSourceLua newclassname|

pageid := each at:1.
pagetitle := each at:2.
oldtext := each at:3.
lua := oldtext .    "I have been attempting String shenanigans here in attempts to get this to work"
lua inspect.
methodTemplatePageId := 'pageId ^', $', '{1}' ,$'. 
methodTemplatePageTitle := 'pageTitle ^', $', '{1}' ,$'. 
methodTemplateLua := 'lua ^', $', '{1}' ,$'. 
methodSourcePageId := methodTemplatePageId format:{pageid}.
methodSourcePageTitle := methodTemplatePageTitle format:{pagetitle}.
methodSourceLua := methodTemplateLua format:{lua}.

newclassname := ('WikitextModule', pageid asString) asSymbol.
WikitextParserModule subclass: newclassname
          instanceVariableNames: ''
          classVariableNames: ''
          poolDictionaries: ''
          category: 'WikitextParser-Modules'.

       (Smalltalk at: newclassname) compile: methodSourcePageId.
       (Smalltalk at: newclassname) compile: methodSourcePageTitle.
       (Smalltalk at: newclassname) compile: methodSourceLua.

       "Transcript show: newclassname; cr."
]

Works fine until 
(Smalltalk at: newclassname) compile: methodSourceLua.

Here's why:

lua ^'-- This module provides easy processing of arguments passed to Scribunto from
-- #invoke. It is intended for use by other Lua modules, and should not be
-- called from #invoke directly.

local libraryUtil = require('libraryUtil    End of block expected ->')
local checkType = libraryUtil.checkType

..........

end

return arguments'

The Method compilation fails.

Should I even attempt to escape this thing?

Remember, I am going to have to do an OSProcess lua call to run this stuff.

I suppose I could just store the PGRow in the class, but that feels weird.

Thanks in advance.




Reply | Threaded
Open this post in threaded view
|

Re: Problem storing Lua code in dynamically created class side method.

Jakob Reschke-2
Hi,

Without having thought it through that much or having understood the whole context:

1. Can't you just replace each ' in your Lua text by '' (two single quotes) before compiling the Smalltalk method string? It escapes the single quotes in the string.
2. Do you really need to create a new Smalltalk class for each row? (That feels weird to me.) Why not create an instance of something for each row and store the collection of all the instances in a well-known place? These instances could have an instance variable to hold the plain Lua code string.
3. If you need Smalltalk classes, you could as well declare a class instance variable and store the unmodified Lua code string there.

    YourClass class
       instanceVariableNames: 'lua'.
    
    lua: aString "class side"
       lua := aString.
    
    lua
       ^ lua
    
    lua "instance side"
       ^ self class lua
    
    YourClass lua: (row at: 3).

Kind regards,
Jakob

Am Mi., 21. Aug. 2019 um 22:49 Uhr schrieb gettimothy via Squeak-dev <[hidden email]>:
Hi all.

I am attempting to store the Wikimedia Lua Modules as Classes in my image. The idea being that when the Parser encounters a Module #invoke I can hand it off to the appropriate class and have it invoke Lua and return content to me.

I have extracted the Wikimedia Modules to a postgres table named 'modules'  and I am hitting that table and iterating over it to dynamically create the classes I need.
The code is a modified version of what I used to scrape the Squeak Wiki.


scrapeModulesToNewClass
|rows |
      rows := ((WikitextParserConnectionPool default executeQuery: 'select page_id, page_title, old_text from modules limit 2') at:1) rows.

rows do: [:each | |pageid pagetitle oldtext lua  methodTemplatePageId methodTemplatePageTitle methodTemplateLua methodSourcePageId methodSourcePageTitle methodSourceLua newclassname|

pageid := each at:1.
pagetitle := each at:2.
oldtext := each at:3.
lua := oldtext .    "I have been attempting String shenanigans here in attempts to get this to work"
lua inspect.
methodTemplatePageId := 'pageId ^', $', '{1}' ,$'. 
methodTemplatePageTitle := 'pageTitle ^', $', '{1}' ,$'. 
methodTemplateLua := 'lua ^', $', '{1}' ,$'. 
methodSourcePageId := methodTemplatePageId format:{pageid}.
methodSourcePageTitle := methodTemplatePageTitle format:{pagetitle}.
methodSourceLua := methodTemplateLua format:{lua}.

newclassname := ('WikitextModule', pageid asString) asSymbol.
WikitextParserModule subclass: newclassname
          instanceVariableNames: ''
          classVariableNames: ''
          poolDictionaries: ''
          category: 'WikitextParser-Modules'.

       (Smalltalk at: newclassname) compile: methodSourcePageId.
       (Smalltalk at: newclassname) compile: methodSourcePageTitle.
       (Smalltalk at: newclassname) compile: methodSourceLua.

       "Transcript show: newclassname; cr."
]

Works fine until 
(Smalltalk at: newclassname) compile: methodSourceLua.

Here's why:

lua ^'-- This module provides easy processing of arguments passed to Scribunto from
-- #invoke. It is intended for use by other Lua modules, and should not be
-- called from #invoke directly.

local libraryUtil = require('libraryUtil    End of block expected ->')
local checkType = libraryUtil.checkType

..........

end

return arguments'

The Method compilation fails.

Should I even attempt to escape this thing?

Remember, I am going to have to do an OSProcess lua call to run this stuff.

I suppose I could just store the PGRow in the class, but that feels weird.

Thanks in advance.





Reply | Threaded
Open this post in threaded view
|

Re: Problem storing Lua code in dynamically created class side method.

Jakob Reschke-2
(including the list in addresses again)

Am Mi., 21. Aug. 2019 um 23:53 Uhr schrieb gettimothy <[hidden email]>:
"1. Can't you just replace each ' in your Lua text by '' (two single quotes) before compiling the Smalltalk method string? It escapes the single quotes in the string."

How?  I tried wrapping it with String Concatenation and it failed to compile.
", oldtext, ".


    methodTemplateLua := 'lua ^ ''{1}'''.
    methodSourceLua := methodTemplateLua format:
       {lua copyReplaceTokens: '''' "four single quotes" with: '''''' "six single quotes"}.

Or for more comfortable reading:

    lua copyReplaceTokens: (String with: $') with: (String with: $' with: $')

Still, I would avoid tinkering with the code and go with the (class) instance variable approach if possible.


Reply | Threaded
Open this post in threaded view
|

Re: Problem storing Lua code in dynamically created class side method.

marcel.taeumel
Hi, there.

#printString on String does the trick:

methodTemplateLua := 'lua ^ {1}'.
methodSourceLua := methodTemplateLua format: { lua printString }.

Best,
Marcel

Am 22.08.2019 00:26:15 schrieb Jakob Reschke <[hidden email]>:

(including the list in addresses again)

Am Mi., 21. Aug. 2019 um 23:53 Uhr schrieb gettimothy <[hidden email]>:
"1. Can't you just replace each ' in your Lua text by '' (two single quotes) before compiling the Smalltalk method string? It escapes the single quotes in the string."

How?  I tried wrapping it with String Concatenation and it failed to compile.
", oldtext, ".


    methodTemplateLua := 'lua ^ ''{1}'''.
    methodSourceLua := methodTemplateLua format:
       {lua copyReplaceTokens: '''' "four single quotes" with: '''''' "six single quotes"}.

Or for more comfortable reading:

    lua copyReplaceTokens: (String with: $') with: (String with: $' with: $')

Still, I would avoid tinkering with the code and go with the (class) instance variable approach if possible.