forked from accordproject/concerto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
77 lines (63 loc) · 2.7 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/robertkrimen/otto"
)
// Context is a Go wrapper around an instance of the Context JavaScript class.
type Context struct {
This *otto.Object
DataService *DataService
IdentityService *IdentityService
}
// NewContext creates a Go wrapper around a new instance of the Context JavaScript class.
func NewContext(vm *otto.Otto, engine *Engine, stub shim.ChaincodeStubInterface) (result *Context) {
logger.Debug("Entering NewContext", vm, engine, stub)
defer func() { logger.Debug("Exiting NewContext", result) }()
// Create a new instance of the JavaScript chaincode class.
temp, err := vm.Call("new composer.Context", nil, engine.This)
if err != nil {
panic(fmt.Sprintf("Failed to create new instance of Context JavaScript class: %v", err))
} else if !temp.IsObject() {
panic("New instance of Context JavaScript class is not an object")
}
object := temp.Object()
// Add a pointer to the Go object into the JavaScript object.
result = &Context{This: object}
err = object.Set("$this", result)
if err != nil {
panic(fmt.Sprintf("Failed to store Go object in Context JavaScript object: %v", err))
}
// Create the services.
result.DataService = NewDataService(vm, result, stub)
result.IdentityService = NewIdentityService(vm, result, stub)
// Bind the methods into the JavaScript object.
result.This.Set("getDataService", result.getDataService)
result.This.Set("getIdentityService", result.getIdentityService)
return result
}
// getDataService returns the data service to use.
func (context *Context) getDataService(call otto.FunctionCall) (result otto.Value) {
logger.Debug("Entering Context.getDataService", call)
defer func() { logger.Debug("Exiting Context.getDataService", result) }()
return context.DataService.This.Value()
}
// getIdentityService returns the identity service to use.
func (context *Context) getIdentityService(call otto.FunctionCall) (result otto.Value) {
logger.Debug("Entering Context.getIdentityService", call)
defer func() { logger.Debug("Exiting Context.getIdentityService", result) }()
return context.IdentityService.This.Value()
}