samualtnorman / hackmud-script-manager

Script manager for game hackmud, with minification, TypeScript support, and player script type definition generation.
https://www.npmjs.com/package/hackmud-script-manager
MIT License
11 stars 5 forks source link

Strange behaviour using functions #170

Closed Braste closed 6 months ago

Braste commented 6 months ago

If I do this

export default (_context: Context, args: unknown) => { 
    $db.us(
        {
            _id: 'harvest'
        },
        {
            _id: 'harvest',
            filter: ['.pub', '.entry'],
            blacklist: ['accenture',
                'arino',
                'blackcore',
                'blackstar',
                'dynamo_corp',
                'jinteki_corp',
                'l4sh',
                'lunar_systems',
                'n_inc',
                'onion',
                'perimeter_systems',
                'ploogle',
                'pwnhub',
                'reinit',
                'rozas',
            ],
            commandRegex: 'with ([a-z`$]+):"([a-z]+)',
            userRegex: '([a-z0-9_]+) (of project|when being)',
            projectRegex: '(date for|continues on|of the|developments on|of project|review of).([a-z0-9.`_]+)',
            passwordRegex: 'strategy.([a-z0-9_]+)'
        }
    )
}

i get down to 74 chars.

If I do this

export default (_context: Context, args: unknown) => { 
    i();
}
function i() {
    $db.us(
        {
            _id: 'harvest'
        },
        {
            _id: 'harvest',
            filter: ['.pub', '.entry'],
            blacklist: ['accenture',
                'arino',
                'blackcore',
                'blackstar',
                'dynamo_corp',
                'jinteki_corp',
                'l4sh',
                'lunar_systems',
                'n_inc',
                'onion',
                'perimeter_systems',
                'ploogle',
                'pwnhub',
                'reinit',
                'rozas',
            ],
            commandRegex: 'with ([a-z`$]+):"([a-z]+)',
            userRegex: '([a-z0-9_]+) (of project|when being)',
            projectRegex: '(date for|continues on|of the|developments on|of project|review of).([a-z0-9.`_]+)',
            passwordRegex: 'strategy.([a-z0-9_]+)'
        }
    )
}

I get 208 chars.

samualtnorman commented 6 months ago

the 208 char script is this:

function(){
//  ["harvest","filter",".pub",".entry","blacklist","accenture","arino","blackcore","blackstar","dynamo_corp","jinteki_corp","l4sh","lunar_systems","n_inc","onion","perimeter_systems","ploogle","pwnhub","reinit","rozas","commandRegex","with ([a-z`$]+):\"([a-z]+)","userRegex","([a-z0-9_]+) (of project|when being)","projectRegex","(date for|continues on|of the|developments on|of project|review of).([a-z0-9.`_]+)","passwordRegex","strategy.([a-z0-9_]+)"] 
let[$,s,t,c,d,i,_,q,S,k,z,I,e,n,p,u,P,T,l,r,B,C,D,N,R,a,f,o]=JSON.parse(#fs.scripts.quine().split`  `[1])
#db.us({_id:$},{_id:$,[s]:[t,c],[d]:[i,_,q,S,k,z,I,e,n,p,u,P,T,l,r],[B]:C,[D]:N,[R]:a,[f]:o})}

This is because the object and array literals are inside a function which means my code thinks it's not safe for quine-cheating. It's generally not safe for quine-cheating because functions can be called multiple times and the object and array literals should evaluate to unique instances every time.

If the whole object was quine-cheated, every time you call the function, it would be the same instance which does not follow normal JavaScript semantics.

Now what my code doesn't know is that the function is called only once so it's not a big deal. I could add a detection for this, however this use-case is mostly solved by fixing/implementing #157.

You can close this issue if you think #157 covers this issue or you keep it open and later decide if this is solved when #157 fixed/implemented.