bring back Parse binding

This commit is contained in:
Adam Babik 2017-11-23 13:37:59 +01:00
parent 4cd362213f
commit 98b3f330af
5 changed files with 86 additions and 0 deletions

View File

@ -179,6 +179,13 @@ func (api *StatusAPI) DiscardTransactions(ids []common.QueuedTxID) map[common.Qu
return api.b.txQueueManager.DiscardTransactions(ids)
}
// JailParse creates a new jail cell context, with the given chatID as identifier.
// New context executes provided JavaScript code, right after the initialization.
// DEPRECATED in favour of CreateAndInitCell.
func (api *StatusAPI) JailParse(chatID string, js string) string {
return api.b.jailManager.Parse(chatID, js)
}
// CreateAndInitCell creates a new jail cell context, with the given chatID as identifier.
// New context executes provided JavaScript code, right after the initialization.
func (api *StatusAPI) CreateAndInitCell(chatID, js string) string {

View File

@ -297,6 +297,11 @@ type JailManager interface {
// CreateCell creates a new jail cell.
CreateCell(chatID string) (JailCell, error)
// Parse creates a new jail cell context, with the given chatID as identifier.
// New context executes provided JavaScript code, right after the initialization.
// DEPRECATED
Parse(chatID, js string) string
// CreateAndInitCell creates a new jail cell and initialize it
// with web3 and other handlers.
CreateAndInitCell(chatID string, code ...string) string

View File

@ -161,6 +161,26 @@ func (j *Jail) CreateAndInitCell(chatID string, code ...string) string {
return j.makeCatalogVariable(cell)
}
// Parse creates a new jail cell context, with the given chatID as identifier.
// New context executes provided JavaScript code, right after the initialization.
// DEPRECATED
func (j *Jail) Parse(chatID, code string) string {
cell, err := j.cell(chatID)
if err != nil {
// cell does not exist
cell, err = j.createAndInitCell(chatID, code)
} else {
// cell already exist, so just execute the code
_, err = cell.Run(code)
}
if err != nil {
return newJailErrorResponse(err)
}
return j.makeCatalogVariable(cell)
}
// makeCatalogVariable provides `catalog` as a global variable.
// TODO(divan): this can and should be implemented outside of jail,
// on a clojure side. Moving this into separate method to nuke it later

View File

@ -323,6 +323,14 @@ func InitJail(js *C.char) {
statusAPI.SetJailBaseJS(C.GoString(js))
}
//Parse creates a new jail cell context and executes provided JavaScript code.
//DEPRECATED in favour of CreateAndInitCell and ExecuteJS.
//export Parse
func Parse(chatID *C.char, js *C.char) *C.char {
res := statusAPI.JailParse(C.GoString(chatID), C.GoString(js))
return C.CString(res)
}
//CreateAndInitCell creates a new jail cell context and executes provided JavaScript code.
//export CreateAndInitCell
func CreateAndInitCell(chatID *C.char, js *C.char) *C.char {

View File

@ -148,6 +148,10 @@ func testExportedAPI(t *testing.T, done chan struct{}) {
"test ExecuteJS",
testExecuteJS,
},
{
"test deprecated Parse",
testJailParseDeprecated,
},
}
for _, test := range tests {
@ -1328,6 +1332,48 @@ func testJailInit(t *testing.T) bool {
return true
}
func testJailParseDeprecated(t *testing.T) bool {
initCode := `
var _status_catalog = {
foo: 'bar'
};
`
InitJail(C.CString(initCode))
extraCode := `
var extraFunc = function (x) {
return x * x;
};
`
rawResponse := Parse(C.CString("CHAT_ID_PARSE_TEST"), C.CString(extraCode))
parsedResponse := C.GoString(rawResponse)
expectedResponse := `{"result": {"foo":"bar"}}`
if !reflect.DeepEqual(expectedResponse, parsedResponse) {
t.Error("expected output not returned from Parse()")
return false
}
// cell already exists but Parse should not complain
rawResponse = Parse(C.CString("CHAT_ID_PARSE_TEST"), C.CString(extraCode))
parsedResponse = C.GoString(rawResponse)
expectedResponse = `{"result": {"foo":"bar"}}`
if !reflect.DeepEqual(expectedResponse, parsedResponse) {
t.Error("expected output not returned from Parse()")
return false
}
// test extraCode
rawResponse = ExecuteJS(C.CString("CHAT_ID_PARSE_TEST"), C.CString(`extraFunc(2)`))
parsedResponse = C.GoString(rawResponse)
expectedResponse = `4`
if !reflect.DeepEqual(expectedResponse, parsedResponse) {
t.Error("expected output not returned from ExecuteJS()")
return false
}
return true
}
func testJailFunctionCall(t *testing.T) bool {
InitJail(C.CString(""))