-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
atom module added, after & tick functions added.
- Loading branch information
1 parent
cc72068
commit 7ca8174
Showing
9 changed files
with
298 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
package main | ||
|
||
import ( | ||
"sync" | ||
|
||
lua "github.com/yuin/gopher-lua" | ||
) | ||
|
||
var ( | ||
intmap = map[string]int64{} | ||
intmux sync.Mutex | ||
nummap = map[string]float64{} | ||
nummux sync.Mutex | ||
) | ||
|
||
// RegAtom is the module loader function. | ||
func RegAtom(ls *lua.LState) { | ||
var mod = ls.RegisterModule("atom", atomfuncs).(*lua.LTable) | ||
_ = mod | ||
} | ||
|
||
var atomfuncs = map[string]lua.LGFunction{ | ||
"intinc": atomintinc, | ||
"intdec": atomintdec, | ||
"intadd": atomintadd, | ||
"intand": atomintand, | ||
"intor": atomintor, | ||
"intxor": atomintxor, | ||
"intget": atomintget, | ||
"intset": atomintset, | ||
"numinc": atomnuminc, | ||
"numdec": atomnumdec, | ||
"numadd": atomnumadd, | ||
"numget": atomnumget, | ||
"numset": atomnumset, | ||
} | ||
|
||
func atomintinc(ls *lua.LState) int { | ||
var name = ls.CheckString(1) | ||
|
||
intmux.Lock() | ||
intmap[name]++ | ||
var v = intmap[name] | ||
intmux.Unlock() | ||
|
||
ls.Push(lua.LNumber(v)) | ||
return 1 | ||
} | ||
|
||
func atomintdec(ls *lua.LState) int { | ||
var name = ls.CheckString(1) | ||
|
||
intmux.Lock() | ||
intmap[name]-- | ||
var v = intmap[name] | ||
intmux.Unlock() | ||
|
||
ls.Push(lua.LNumber(v)) | ||
return 1 | ||
} | ||
|
||
func atomintadd(ls *lua.LState) int { | ||
var name = ls.CheckString(1) | ||
var arg = ls.CheckInt64(2) | ||
|
||
intmux.Lock() | ||
intmap[name] += arg | ||
var v = intmap[name] | ||
intmux.Unlock() | ||
|
||
ls.Push(lua.LNumber(v)) | ||
return 1 | ||
} | ||
|
||
func atomintand(ls *lua.LState) int { | ||
var name = ls.CheckString(1) | ||
var arg = ls.CheckInt64(2) | ||
|
||
intmux.Lock() | ||
intmap[name] &= arg | ||
var v = intmap[name] | ||
intmux.Unlock() | ||
|
||
ls.Push(lua.LNumber(v)) | ||
return 1 | ||
} | ||
|
||
func atomintor(ls *lua.LState) int { | ||
var name = ls.CheckString(1) | ||
var arg = ls.CheckInt64(2) | ||
|
||
intmux.Lock() | ||
intmap[name] |= arg | ||
var v = intmap[name] | ||
intmux.Unlock() | ||
|
||
ls.Push(lua.LNumber(v)) | ||
return 1 | ||
} | ||
|
||
func atomintxor(ls *lua.LState) int { | ||
var name = ls.CheckString(1) | ||
var arg = ls.CheckInt64(2) | ||
|
||
intmux.Lock() | ||
intmap[name] ^= arg | ||
var v = intmap[name] | ||
intmux.Unlock() | ||
|
||
ls.Push(lua.LNumber(v)) | ||
return 1 | ||
} | ||
|
||
func atomintget(ls *lua.LState) int { | ||
var name = ls.CheckString(1) | ||
|
||
intmux.Lock() | ||
var v = intmap[name] | ||
intmux.Unlock() | ||
|
||
ls.Push(lua.LNumber(v)) | ||
return 1 | ||
} | ||
|
||
func atomintset(ls *lua.LState) int { | ||
var name = ls.CheckString(1) | ||
var arg = ls.CheckInt64(2) | ||
|
||
intmux.Lock() | ||
intmap[name] = arg | ||
intmux.Unlock() | ||
|
||
return 0 | ||
} | ||
|
||
func atomnuminc(ls *lua.LState) int { | ||
var name = ls.CheckString(1) | ||
|
||
nummux.Lock() | ||
nummap[name]++ | ||
var v = nummap[name] | ||
nummux.Unlock() | ||
|
||
ls.Push(lua.LNumber(v)) | ||
return 1 | ||
} | ||
|
||
func atomnumdec(ls *lua.LState) int { | ||
var name = ls.CheckString(1) | ||
|
||
nummux.Lock() | ||
nummap[name]-- | ||
var v = nummap[name] | ||
nummux.Unlock() | ||
|
||
ls.Push(lua.LNumber(v)) | ||
return 1 | ||
} | ||
|
||
func atomnumadd(ls *lua.LState) int { | ||
var name = ls.CheckString(1) | ||
var arg = ls.CheckNumber(2) | ||
|
||
nummux.Lock() | ||
nummap[name] += float64(arg) | ||
var v = nummap[name] | ||
nummux.Unlock() | ||
|
||
ls.Push(lua.LNumber(v)) | ||
return 1 | ||
} | ||
|
||
func atomnumget(ls *lua.LState) int { | ||
var name = ls.CheckString(1) | ||
|
||
nummux.Lock() | ||
var v = nummap[name] | ||
nummux.Unlock() | ||
|
||
ls.Push(lua.LNumber(v)) | ||
return 1 | ||
} | ||
|
||
func atomnumset(ls *lua.LState) int { | ||
var name = ls.CheckString(1) | ||
var arg = ls.CheckInt64(2) | ||
|
||
nummux.Lock() | ||
nummap[name] = float64(arg) | ||
nummux.Unlock() | ||
|
||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.