<commit_before>#   Copyright 2011-2012 <NAME>, INFICON, and other SCION contributors
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.



define ["util/set/ArraySet","scxml/state-kinds-enum","scxml/event","util/reduce","scxml/setup-default-opts","scxml/scxml-dynamic-name-match-transition-selector","logger"],(ArraySet,stateKinds,Event,reduce,setupDefaultOpts,scxmlPrefixTransitionSelector,logger) ->

    #imports

    flatten = (l) ->
        a = []
        for list in l
            a = a.concat(list)

        return a


    #technique adapted from http://javascript.crockford.com/prototypal.html
    create = (o) ->
        if Object.create
            Object.create(o)
        else
            F = ->
            F.prototype = o
            return new F()

    # -> Priority: Source-Child 
    getTransitionWithHigherSourceChildPriority = (model) ->
        ([t1,t2]) ->
            """
            compare transitions based first on depth, then based on document order
            """
            if model.getDepth(t1.source) < model.getDepth(t2.source)
                return t2
            else if model.getDepth(t2.source) < model.getDepth(t1.source)
                return t1
            else
                if t1.documentOrder < t2.documentOrder
                    return t1
                else
                    return t2

    class SCXMLInterpreter

        constructor : (@model,@opts={}) ->

            if @opts.printTrace
                logger.trace "initializing SCXML interpreter with opts:"
                for own k,v of opts
                    v = if typeof v is "function" then v.toString() else v
                    logger.trace k,v

            #default args
            #@opts.onlySelectFromBasicStates
            #@opts.printTrace = true
            #@opts.evaluationContext    #sets the this object for script evaluation
            #@opts.transitionSelector ?= defaultTransitionSelector()
            #@opts.model ?= m
            #@opts.TransitionSet ?= ArraySet
            #@opts.StateSet ?= ArraySet
            #@opts.BasicStateSet ?= ArraySet
            @opts.StateIdSet ?= ArraySet
            @opts.EventSet ?= ArraySet
            @opts.TransitionPairSet ?= ArraySet
            @opts.priorityComparisonFn ?= getTransitionWithHigherSourceChildPriority(@opts.model)
            @opts.globalEval ?= window?.executeScript or eval       #we parameterize this in case we want to use, e.g. jquery.globalEval

            @_configuration = new @opts.BasicStateSet() #full configuration, or basic configuration? what kind of set implementation?
            @_historyValue = {}
            @_innerEventQueue = []
            @_isInFinalState = false
            @_datamodel = create @model.datamodel       #FIXME: should these be global, or declared at the level of the big step, like the eventQueue?
            @_timeoutMap = {}

        
        start: ->
            #perform big step without events to take all default transitions and reach stable initial state
            if @opts.printTrace then logger.trace("performing initial big step")
            @_configuration.add(@model.root.initial)

            #eval top-level scripts
            #we treat these differently than other scripts. they get evaled in global scope, and without explicit scripting interface
            #this is necessary in order to, e.g., allow js function declarations that are visible to scxml script tags later.
            for script in @model.scripts
                `with(this._datamodel){ this.opts.globalEval.call(null,script) }`

            #initialize top-level datamodel expressions. simple eval
            for k,v of @_datamodel
                if v then @_datamodel[k] = eval(v)

            @_performBigStep()
            return @getConfiguration()
<commit_msg>Updated datamodel evaluation to better handle object literal declaration.<commit_after>#   Copyright 2011-2012 <NAME>, INFICON, and other SCION contributors
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.



define ["util/set/ArraySet","scxml/state-kinds-enum","scxml/event","util/reduce","scxml/setup-default-opts","scxml/scxml-dynamic-name-match-transition-selector","logger"],(ArraySet,stateKinds,Event,reduce,setupDefaultOpts,scxmlPrefixTransitionSelector,logger) ->

    #imports

    flatten = (l) ->
        a = []
        for list in l
            a = a.concat(list)

        return a


    #technique adapted from http://javascript.crockford.com/prototypal.html
    create = (o) ->
        if Object.create
            Object.create(o)
        else
            F = ->
            F.prototype = o
            return new F()

    # -> Priority: Source-Child 
    getTransitionWithHigherSourceChildPriority = (model) ->
        ([t1,t2]) ->
            """
            compare transitions based first on depth, then based on document order
            """
            if model.getDepth(t1.source) < model.getDepth(t2.source)
                return t2
            else if model.getDepth(t2.source) < model.getDepth(t1.source)
                return t1
            else
                if t1.documentOrder < t2.documentOrder
                    return t1
                else
                    return t2

    class SCXMLInterpreter

        constructor : (@model,@opts={}) ->

            if @opts.printTrace
                logger.trace "initializing SCXML interpreter with opts:"
                for own k,v of opts
                    v = if typeof v is "function" then v.toString() else v
                    logger.trace k,v

            #default args
            #@opts.onlySelectFromBasicStates
            #@opts.printTrace = true
            #@opts.evaluationContext    #sets the this object for script evaluation
            #@opts.transitionSelector ?= defaultTransitionSelector()
            #@opts.model ?= m
            #@opts.TransitionSet ?= ArraySet
            #@opts.StateSet ?= ArraySet
            #@opts.BasicStateSet ?= ArraySet
            @opts.StateIdSet ?= ArraySet
            @opts.EventSet ?= ArraySet
            @opts.TransitionPairSet ?= ArraySet
            @opts.priorityComparisonFn ?= getTransitionWithHigherSourceChildPriority(@opts.model)
            @opts.globalEval ?= window?.executeScript or eval       #we parameterize this in case we want to use, e.g. jquery.globalEval

            @_configuration = new @opts.BasicStateSet() #full configuration, or basic configuration? what kind of set implementation?
            @_historyValue = {}
            @_innerEventQueue = []
            @_isInFinalState = false
            @_datamodel = create @model.datamodel       #FIXME: should these be global, or declared at the level of the big step, like the eventQueue?
            @_timeoutMap = {}

        
        start: ->
            #perform big step without events to take all default transitions and reach stable initial state
            if @opts.printTrace then logger.trace("performing initial big step")
            @_configuration.add(@model.root.initial)

            #eval top-level scripts
            #we treat these differently than other scripts. they get evaled in global scope, and without explicit scripting interface
            #this is necessary in order to, e.g., allow js function declarations that are visible to scxml script tags later.
            for script in @model.scripts
                `with(this._datamodel){ this.opts.globalEval.call(null,script) }`

            #initialize top-level datamodel expressions. simple eval
            for k,v of @_datamodel
                if v then @_datamodel[k] = eval("(#{v})")

            @_performBigStep()
            return @getConfiguration()

{'fraction_non_alphanumeric': 0.0851063829787234, 'fraction_numerical': 0.005520414031052329, 'mean_word_length': 2.1034975017844397, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 14}, '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': '7474218', 'n_tokens_mistral': 2293, 'n_tokens_neox': 2197, 'n_words': 716}