{"version":3,"sources":["scope.ts"],"sourcesContent":["import { ProtocolDataType } from \"../compiler/protocol_types.ts\";\nimport { Runtime } from \"../runtime/runtime.ts\";\nimport { Compiler } from \"../compiler/compiler.ts\";\nimport { Endpoint } from \"../types/addressing.ts\";\nimport type { dxb_header } from \"../utils/global_types.ts\";\nimport { Pointer } from \"../runtime/pointers.ts\";\nimport {decompile} from \"../wasm/adapter/pkg/datex_wasm.js\";\n\n/** <std:Scope> */\nexport class Scope<T=any> {\n\n    // injected variable names != arguments\n    internal_vars:any[] = []\n    // compiled dxb\n    compiled: ArrayBuffer;\n\n    // object containing all variables from a parent scope\n    parent_variables:any;\n\n    // decompiled dxb (unformatted and formatted)\n    private _decompiled = \"### DATEX ###\";\n    private _decompiled_f = \"### DATEX ###\";\n\n    constructor(internal_vars:any[], compiled:ArrayBuffer, generate_decompiled=true) {\n        this.internal_vars = internal_vars;\n\n        // make internal vars persistent even if currently not loaded in JS (TODO: how to handle this case in the future?)\n        for (const internal_var of this.internal_vars) {\n            if (internal_var instanceof Pointer) {\n                internal_var.is_persistent = true;\n            }\n        }\n\n        this.compiled = compiled;\n        // decompile\n        if (generate_decompiled) {\n            try {\n                this._decompiled_f = decompile(new Uint8Array(this.compiled), true, false, true);\n                this._decompiled   = decompile(new Uint8Array(this.compiled), false, false, true);\n            }\n            catch {\n                // TODO: fix, happens with old endpoint format in datex-core\n                // logger.debug(\"could not generated decompiled scope script\")\n            }\n            \n        }\n    }\n\n    // run the dxb with arguments, executed by a specific endpoint\n    public execute(executed_by:Endpoint, context?:any, it?:any):Promise<T> {\n        \n        // generate new header using executor scope header\n        const header:dxb_header = {\n            sender: executed_by,\n            type: ProtocolDataType.LOCAL,\n            executable: true,\n            sid: Compiler.generateSID()\n        }\n\n        // create scope TODO fix internal_vars array or object (force converting to object for now)\n        const scope = Runtime.createNewInitialScope(header, {...this.internal_vars}, context, it);\n        // update scope buffers\n        Runtime.updateScope(scope, this.compiled, header)\n        // execute scope\n        return Runtime.simpleScopeExecution(scope)\n    }\n\n    get decompiled():string {\n        return this._decompiled;\n    }\n    get decompiled_formatted():string {\n        return this._decompiled_f;\n    }\n\n\n    bodyToString(formatted=false, parentheses=true, spaces = '  '){\n        return (parentheses?'(':'') + (formatted&&parentheses ? \"\\n\":\"\") + (formatted ? this.decompiled_formatted?.replace(/^/gm, spaces) : this.decompiled).replace(/ *$/,'') + (parentheses?')':'')\n    }\n\n    toString(formatted=false, spaces = '  '){\n        return `scope ${this.bodyToString(formatted, true, spaces)}`;\n    }\n}\n"],"names":["ProtocolDataType","Runtime","Compiler","Pointer","decompile","Scope","internal_vars","compiled","parent_variables","_decompiled","_decompiled_f","constructor","generate_decompiled","internal_var","is_persistent","Uint8Array","execute","executed_by","context","it","header","sender","type","LOCAL","executable","sid","generateSID","scope","createNewInitialScope","updateScope","simpleScopeExecution","decompiled","decompiled_formatted","bodyToString","formatted","parentheses","spaces","replace","toString"],"mappings":"AAAA,SAASA,gBAAgB,QAAQ,gCAAgC;AACjE,SAASC,OAAO,QAAQ,wBAAwB;AAChD,SAASC,QAAQ,QAAQ,0BAA0B;AAGnD,SAASC,OAAO,QAAQ,yBAAyB;AACjD,SAAQC,SAAS,QAAO,oCAAoC;AAG5D,OAAO,MAAMC;IAGTC,gBAAsB,EAAE,CAAA;IAExBC,SAAsB;IAGtBC,iBAAqB;IAGbC,cAAc,gBAAgB;IAC9BC,gBAAgB,gBAAgB;IAExCC,YAAYL,aAAmB,EAAEC,QAAoB,EAAEK,sBAAoB,CAAA,CAAI,CAAE;QAI7E,KAAK,IAAMC,iBAHX,IAAI,CAACP,aAAa,GAAGA,eAGM,IAAI,CAACA,aAAa,EACrCO,wBAAwBV,WACxBU,CAAAA,aAAaC,aAAa,GAAG,CAAA,CAAG;QAMxC,IAFA,IAAI,CAACP,QAAQ,GAAGA,UAEZK,qBACA,IAAI;YACA,IAAI,CAACF,aAAa,GAAGN,UAAU,IAAIW,WAAW,IAAI,CAACR,QAAQ,GAAG,CAAA,GAAM,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