Merge pull request #472 from status-im/bugfix/bring-back-parse
Bring back Parse binding
This commit is contained in:
commit
69276386d2
|
@ -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 {
|
||||
|
|
|
@ -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 in favour of CreateAndInitCell.
|
||||
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
|
||||
|
|
|
@ -161,6 +161,30 @@ 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 in favour of CreateAndInitCell.
|
||||
func (j *Jail) Parse(chatID, code string) string {
|
||||
cell, err := j.cell(chatID)
|
||||
if err != nil {
|
||||
// cell does not exist, so create and init it
|
||||
cell, err = j.createAndInitCell(chatID, code)
|
||||
} else {
|
||||
// cell already exists, so just reinit it
|
||||
err = j.initCell(cell)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return newJailErrorResponse(err)
|
||||
}
|
||||
|
||||
if _, err = cell.Run(code); 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
|
||||
|
|
|
@ -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.
|
||||
//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 {
|
||||
|
|
46
lib/utils.go
46
lib/utils.go
|
@ -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(""))
|
||||
|
||||
|
|
Loading…
Reference in New Issue