From 98b3f330afd433f2784d0db8fab9f3fe24ca92fb Mon Sep 17 00:00:00 2001 From: Adam Babik Date: Thu, 23 Nov 2017 13:37:59 +0100 Subject: [PATCH 1/3] bring back Parse binding --- geth/api/api.go | 7 +++++++ geth/common/types.go | 5 +++++ geth/jail/jail.go | 20 +++++++++++++++++++ lib/library.go | 8 ++++++++ lib/utils.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+) diff --git a/geth/api/api.go b/geth/api/api.go index 8f18d7738..67c799a3b 100644 --- a/geth/api/api.go +++ b/geth/api/api.go @@ -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 { diff --git a/geth/common/types.go b/geth/common/types.go index 52a55fb8c..ce13157ff 100644 --- a/geth/common/types.go +++ b/geth/common/types.go @@ -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 diff --git a/geth/jail/jail.go b/geth/jail/jail.go index c887178af..012ca916a 100644 --- a/geth/jail/jail.go +++ b/geth/jail/jail.go @@ -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 diff --git a/lib/library.go b/lib/library.go index 70a4f9f21..86b32d612 100644 --- a/lib/library.go +++ b/lib/library.go @@ -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 { diff --git a/lib/utils.go b/lib/utils.go index b6362ff63..e31e26aae 100644 --- a/lib/utils.go +++ b/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("")) From 47d16bffd01f35dc784d3ff0d621c382df5971c3 Mon Sep 17 00:00:00 2001 From: Adam Babik Date: Thu, 23 Nov 2017 13:47:20 +0100 Subject: [PATCH 2/3] fix Parse --- geth/jail/jail.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/geth/jail/jail.go b/geth/jail/jail.go index 012ca916a..574eeeb4a 100644 --- a/geth/jail/jail.go +++ b/geth/jail/jail.go @@ -167,17 +167,21 @@ func (j *Jail) CreateAndInitCell(chatID string, code ...string) string { func (j *Jail) Parse(chatID, code string) string { cell, err := j.cell(chatID) if err != nil { - // cell does not exist + // cell does not exist, so create and init it cell, err = j.createAndInitCell(chatID, code) } else { - // cell already exist, so just execute the code - _, err = cell.Run(code) + // 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) } From a22638f4215d865a745aff34b91cf5151cd3c644 Mon Sep 17 00:00:00 2001 From: Adam Babik Date: Thu, 23 Nov 2017 13:51:52 +0100 Subject: [PATCH 3/3] be consistent with comments --- geth/common/types.go | 2 +- geth/jail/jail.go | 2 +- lib/library.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/geth/common/types.go b/geth/common/types.go index ce13157ff..0d3689cc9 100644 --- a/geth/common/types.go +++ b/geth/common/types.go @@ -299,7 +299,7 @@ type JailManager interface { // Parse creates a new jail cell context, with the given chatID as identifier. // New context executes provided JavaScript code, right after the initialization. - // DEPRECATED + // DEPRECATED in favour of CreateAndInitCell. Parse(chatID, js string) string // CreateAndInitCell creates a new jail cell and initialize it diff --git a/geth/jail/jail.go b/geth/jail/jail.go index 574eeeb4a..6cb2b7c8b 100644 --- a/geth/jail/jail.go +++ b/geth/jail/jail.go @@ -163,7 +163,7 @@ func (j *Jail) CreateAndInitCell(chatID string, code ...string) string { // Parse creates a new jail cell context, with the given chatID as identifier. // New context executes provided JavaScript code, right after the initialization. -// DEPRECATED +// DEPRECATED in favour of CreateAndInitCell. func (j *Jail) Parse(chatID, code string) string { cell, err := j.cell(chatID) if err != nil { diff --git a/lib/library.go b/lib/library.go index 86b32d612..4fd0ff29b 100644 --- a/lib/library.go +++ b/lib/library.go @@ -324,7 +324,7 @@ func InitJail(js *C.char) { } //Parse creates a new jail cell context and executes provided JavaScript code. -//DEPRECATED in favour of CreateAndInitCell and ExecuteJS. +//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))