<commit_before>/*

Emglken VM
==========

Copyright (c) 2022 <NAME>
MIT licenced
https://github.com/curiousdannii/emglken

*/

import EmglkenFS from './emglkenfs.js'

const base_options = {
    arguments: ['storyfile'],
}

export default class EmglkenVM
{
    // Store the data and options
    init(data, options)
    {
        this.data = data
        this.options = Object.assign({}, base_options, this.default_options(), options)
    }

    // Start GlkOte and the vmcore
    async start()
    {
        const encoder = new TextEncoder()
        let buffer = ''

        const Module = {
            arguments: this.options.show_help ? ['-help'] : this.options.arguments,
            emglken_stdin_buffers: [],
            emglken_stdin_ready() {},
            print: data =>
            {
                if (buffer === '' && data !== '' && !data.startsWith('{'))
                {
                    console.log(data)
                }
                else
                {
                    buffer += data
                    if (data.endsWith('}'))
                    {
                        let obj
                        try { obj = JSON.parse(buffer) } catch (e) {}
                        if (obj) {
                            buffer = ''
                            // Store the usage of a fileref prompt request
                            if (obj.specialinput && obj.specialinput.type === 'fileref_prompt')
                            {
                                this.last_fr_usage = obj.specialinput.filetype
                            }
                            this.options.GlkOte.update(obj)
                        }
                    }
                }
            },
            preRun: () =>
            {
                const FS = Module.FS
                this.EFS = new EmglkenFS(this)
                FS.mkdir('/emglken')
                FS.mount(this.EFS, {}, '/emglken')
                FS.chdir('/emglken')
            },
            wasmBinary: this.options.wasmBinary,
        }
        this.Module = Module

        this.options.accept = data => {
            if (data.type === 'specialresponse' && data.response === 'fileref_prompt' && data.value)
            {
                // electrofs.js returns a full path, so register it with EmglkenFS, and return a fake filename
                if (this.EFS.streaming)
                {
                    data.value = this.EFS.register_filename(data.value.filename, this.last_fr_usage)
                }
                // Convert a dialog.js provided fileref into something Remglk can understand
                else
                {
                    data.value = data.value.filename
                }
            }

            const json_data = JSON.stringify(data)
            const buffer = encoder.encode(json_data)
            Module.emglken_stdin_buffers.push(buffer)
            Module.emglken_stdin_ready()
        }

        await this.options.vmcore(Module)
        this.options.GlkOte.init(this.options)
    }
}<commit_msg>Lectrote gets tripped up by import.meta.url not existing in its CJS bundle, so set a fake Module.locateFile()<commit_after>/*

Emglken VM
==========

Copyright (c) 2022 <NAME>
MIT licenced
https://github.com/curiousdannii/emglken

*/

import EmglkenFS from './emglkenfs.js'

const base_options = {
    arguments: ['storyfile'],
}

export default class EmglkenVM
{
    // Store the data and options
    init(data, options)
    {
        this.data = data
        this.options = Object.assign({}, base_options, this.default_options(), options)
    }

    // Start GlkOte and the vmcore
    async start()
    {
        const encoder = new TextEncoder()
        let buffer = ''

        const Module = {
            arguments: this.options.show_help ? ['-help'] : this.options.arguments,
            emglken_stdin_buffers: [],
            emglken_stdin_ready() {},
            // Fake locateFile so that Lectrote doesn't get tripped up on import.meta.url not being handled in CJS properly
            locateFile() {},
            print: data =>
            {
                if (buffer === '' && data !== '' && !data.startsWith('{'))
                {
                    console.log(data)
                }
                else
                {
                    buffer += data
                    if (data.endsWith('}'))
                    {
                        let obj
                        try { obj = JSON.parse(buffer) } catch (e) {}
                        if (obj) {
                            buffer = ''
                            // Store the usage of a fileref prompt request
                            if (obj.specialinput && obj.specialinput.type === 'fileref_prompt')
                            {
                                this.last_fr_usage = obj.specialinput.filetype
                            }
                            this.options.GlkOte.update(obj)
                        }
                    }
                }
            },
            preRun: () =>
            {
                const FS = Module.FS
                this.EFS = new EmglkenFS(this)
                FS.mkdir('/emglken')
                FS.mount(this.EFS, {}, '/emglken')
                FS.chdir('/emglken')
            },
            wasmBinary: this.options.wasmBinary,
        }
        this.Module = Module

        this.options.accept = data => {
            if (data.type === 'specialresponse' && data.response === 'fileref_prompt' && data.value)
            {
                // electrofs.js returns a full path, so register it with EmglkenFS, and return a fake filename
                if (this.EFS.streaming)
                {
                    data.value = this.EFS.register_filename(data.value.filename, this.last_fr_usage)
                }
                // Convert a dialog.js provided fileref into something Remglk can understand
                else
                {
                    data.value = data.value.filename
                }
            }

            const json_data = JSON.stringify(data)
            const buffer = encoder.encode(json_data)
            Module.emglken_stdin_buffers.push(buffer)
            Module.emglken_stdin_ready()
        }

        await this.options.vmcore(Module)
        this.options.GlkOte.init(this.options)
    }
}
{'fraction_non_alphanumeric': 0.10295278698878889, 'fraction_numerical': 0.0012632243802305385, 'mean_word_length': 1.2008339124391938, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1237025', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1626, 'n_words': 356}