/**
 * Copyright 2018 Twitter, Inc.
 * Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0
 */ import { SqrlExecutionState } from "./SqrlExecutionState" ; import { FeatureMap , Manipulator } from "../api/execute" ; import SqrlSourcePrinter from "../compile/SqrlSourcePrinter" ; import { ExecutableSpec , RuleSpecMap } from "../api/spec" ; import { JsExecutionContext } from "../node/JsExecutionContext" ; import { Context } from "../api/ctx" ; const DEFAULT_FEATURE_TIMEOUT_MS = 1000 ; function zipToObjects ( keys , objects ) { const objectNames = Object . keys ( objects ) ; const rv = { } ; keys . forEach ( ( key , idx ) => { rv [ key ] = { } ; objectNames . forEach ( ( obj ) => { rv [ key ] [ obj ] = objects [ obj ] [ idx ] ; } ) ; } ) ; return rv ; } export class SqrlExecutable { version : string ; parentVersion : string ; slotCount : number ; parent : SqrlExecutable ; sourcePrinter : SqrlSourcePrinter ; slotNames : string [ ] ; slotJs : string [ ] ; slotCallback : ( ( ) => Promise < any > ) [ ] ; slotCosts : number [ ] ; slotRecursiveCosts : number [ ] ; ruleSpecs : RuleSpecMap ; requiredSlots : number [ ] ; constructor ( executionContext , props ) { const { slotNames , slotJs } = props ; this . ruleSpecs = props . rules ; this . slotNames = slotNames ; this . slotCount = slotJs . length ; this . slotCosts = props . slotCosts ; this . slotRecursiveCosts = props . slotRecursiveCosts ; this . slotJs = slotJs ; this . requiredSlots = [ ] ; this . slotCallback = slotJs . map ( ( js , index ) => { if ( js === null ) { this . requiredSlots . push ( index ) ; return null ; } return executionContext . compileSlotJs ( js ) ; } ) ; this . sourcePrinter = new SqrlSourcePrinter ( { slotNames , slotJs , } ) ; } async startExecution ( ctx , options = { } ) { const manipulator = options . manipulator || null ; const state = new SqrlExecutionState ( ctx , this . slotCallback , this . slotNames , manipulator , { featureTimeout : options . featureTimeoutMs || DEFAULT_FEATURE_TIMEOUT_MS , sourcePrinter : this . sourcePrinter , slotCost : this . slotCosts , requiredSlots : this . requiredSlots , ruleSpecs : this . ruleSpecs , } , options . inputs || { } ) ; await state . fetchClock ( ) ; return state ; } dumpSlotInformation ( ) { return zipToObjects ( this . slotNames , { cost : this . slotCosts , recursiveCost : this . slotRecursiveCosts , } ) ; } getNames ( ) { return this . slotNames ; } getRequiredSlotNames ( ) { return this . requiredSlots . map ( ( idx ) => this . slotNames [ idx ] ) ; } } export default SqrlExecutable ;