This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 725
/
container.go
85 lines (70 loc) · 3.15 KB
/
container.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
78
79
80
81
82
83
84
85
/*
* 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 (
duktape "gopkg.in/olebedev/go-duktape.v3"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
// Container is a Go wrapper around an instance of the Container JavaScript class.
type Container struct {
VM *duktape.Context
LoggingService *LoggingService
}
// NewContainer creates a Go wrapper around a new instance of the Container JavaScript class.
func NewContainer(vm *duktape.Context, stub shim.ChaincodeStubInterface) (result *Container) {
logger.Debug("Entering NewContainer", vm)
defer func() { logger.Debug("Exiting NewContainer", result) }()
// Ensure the JavaScript stack is reset.
defer vm.SetTop(vm.GetTop())
// Create the new container.
result = &Container{VM: vm}
// Create the services.
result.LoggingService = NewLoggingService(vm, result, stub)
// Create a new instance of the JavaScript Container class.
vm.PushGlobalObject() // [ global ]
vm.GetPropString(-1, "composer") // [ global composer ]
vm.GetPropString(-1, "Container") // [ global composer Container ]
err := vm.Pnew(0) // [ global composer theContainer ]
if err != nil {
panic(err)
}
vm.PushGlobalStash() // [ global composer theContainer stash ]
vm.Dup(-2) // [ global composer theContainer stash theContainer ]
vm.PutPropString(-2, "container") // [ global composer theContainer stash ]
vm.Pop() // [ global composer theContainer ]
// Bind the methods into the JavaScript object.
vm.PushGoFunction(result.getVersion) // [ global composer theContainer getVersion ]
vm.PutPropString(-2, "getVersion") // [ global composer theContainer ]
vm.PushGoFunction(result.getLoggingService) // [ global composer theContainer getLoggingService ]
vm.PutPropString(-2, "getLoggingService") // [ global composer theContainer ]
// Return the new container.
return result
}
// getVersion returns the current version of the chaincode.
func (container *Container) getVersion(vm *duktape.Context) (result int) {
logger.Debug("Entering Container.getVersion", vm)
defer func() { logger.Debug("Exiting Container.getVersion", result) }()
// Return the chaincode version.
vm.PushString(version)
return 1
}
// getLoggingService returns the logging service to use.
func (container *Container) getLoggingService(vm *duktape.Context) (result int) {
logger.Debug("Entering Container.getLoggingService", vm)
defer func() { logger.Debug("Exiting Container.getLoggingService", result) }()
// Return the JavaScript object from the global stash.
vm.PushGlobalStash()
vm.GetPropString(-1, "loggingService")
return 1
}