diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-03-24 11:39:25 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2020-03-24 11:39:25 +0000 |
| commit | 677c0d2f01e14bbeca1583ec7878d80c71c3aa68 (patch) | |
| tree | cc20e04697da590b546de370599eeaaef647d20c /core/base/op.moon | |
| parent | internals/plugin-guide: first draft (diff) | |
| download | alive-677c0d2f01e14bbeca1583ec7878d80c71c3aa68.tar.gz alive-677c0d2f01e14bbeca1583ec7878d80c71c3aa68.zip | |
Value -> Value/Event/IO-Stream
Close 12
Diffstat (limited to 'core/base/op.moon')
| -rw-r--r-- | core/base/op.moon | 66 |
1 files changed, 37 insertions, 29 deletions
diff --git a/core/base/op.moon b/core/base/op.moon index a8da100..4880afd 100644 --- a/core/base/op.moon +++ b/core/base/op.moon @@ -2,16 +2,16 @@ -- Persistent expression Operator. -- -- @classmod Op -import Value from require 'core.value' -ident = (tbl) -> - mt = getmetatable tbl - setmetatable tbl, nil - - id = tostring tbl - - setmetatable tbl, mt - id\sub #'table: 0x' +deepcopy = (val) -> + switch type val + when 'number', 'string', 'boolean' + val + when 'table' + assert (not getmetatable {}), "state should only contain simple tables!" + {(deepcopy k), (deepcopy v) for k,v in pairs val} + else + error "state cannot contain values of type '#{type val}'" class Op --- members @@ -33,23 +33,30 @@ class Op -- Used to wrap insulate eval-cycles from each other. The copy does not have -- `inputs` set, since it is expected that this is (re)set in `setup`. -- - -- @treturn Value + -- @treturn Op fork: => - with setmetatable {}, getmetatable @ - .state = {k,v for k,v in pairs @state} if @state - .out = @out\fork! if @out + out = if @out then @out\fork + state = if @state then deepcopy state + @@ out, state + + --- internal state of this Op. + -- + -- This may be any simple Lua value, including Lua tables, as long as it has + -- no metatables, multiple references/loops, userdata etc. + -- + -- @tfield table state - --- `Value` instance representing this Op's computed output value. + --- `Stream` instance representing this Op's computed output value. -- - -- Must be set to a `Value` instance once `setup` finishes. Must not change - -- type, be removed or replaced outside of `new` and `setup`. Should have a - -- value assigned via `set` or the `Value` constructor once `tick` is - -- called the first time. If `out`'s value is not initialized in `new` - -- or `setup`, the implementation must make sure `tick``(true)` is called at - -- least on the first eval-cycle the Op goes through, e.g. by using an - -- `Input.value`. + -- Must be set to a `Stream` instance once `setup` finishes. Must not change + -- type, be removed or replaced outside of `new` and `setup`. If it is a + -- `ValueStream`, it should have a value assigned via `set` or the + -- constructor once `tick` is called the first time. If `out`'s value is not + -- initialized in `new` or `setup`, the implementation must make sure + -- `tick``(true)` is called at -- least on the first eval-cycle the Op goes + -- through, e.g. by using an `Input.value`. -- - -- @tfield Value out + -- @tfield Stream out --- table containing `Input`s to this Op. -- @@ -66,10 +73,14 @@ class Op --- construct a new instance. -- - -- The super-constructor can be used to construct a `Value` instance in `out`. + -- The optional parameters `out` and `state` are used by `fork` to duplicate + -- an instance. If the constructor is overriden, these parameters must be + -- forwarded to the superconstructor unchanged. -- -- @function new -- @classmethod + -- @tparam ?Stream out `out` + -- @tparam ?table state `state` --- parse arguments and patch self. -- @@ -109,12 +120,9 @@ class Op -- type is not known at this time. -- -- @classmethod - -- @tparam[opt] string type the type-name for `out` - -- @tparam[optchain] any init the initial value for `out` - new: (type, init) => - @state = {} - if type - @out = Value type, init + -- @tparam ?Stream out `out` + -- @tparam ?table state `state` + new: (@out, @state) => do_setup = (old, cur) -> for k, cur_val in pairs cur |
