more memory free, change json dep to fluencelabs repo

This commit is contained in:
DieMyst 2019-03-19 12:59:20 +03:00
parent 6aec6d050f
commit 10810ce9b5
6 changed files with 47 additions and 13 deletions

View File

@ -14,7 +14,6 @@ export class GameManager {
registeredPlayers: u64 = 0;
playerIds: u64[] = new Array();
playerBalance: Map<u64, u64> = new Map<u64, u64>();
encoder: JSONEncoder = new JSONEncoder();
constructor() {
NativeMath.seedRandom(SEED);
@ -35,7 +34,8 @@ export class GameManager {
this.registeredPlayers = this.registeredPlayers + 1;
return response.serialize();
let resultStr = response.serialize();
return resultStr;
}
roll(playerId: u64, betPlacement: u8, betSize: u64): string {
@ -67,11 +67,12 @@ export class GameManager {
newBalance = balance - betSize;
}
this.playerBalance.delete(playerId);
this.playerBalance.set(playerId, newBalance);
let response = new RollResponse(outcome, newBalance);
return response.serialize();
let resultStr = response.serialize();
memory.free(changetype<usize>(response));
return resultStr;
}
getBalance(playerId: u64): string {

View File

@ -8,6 +8,7 @@ let gameManager = new GameManager();
export function handler(requestBytes: Uint8Array): string {
let request: Request = decode(requestBytes);
// let request: Request = new RollRequest(0, 1, 2);
if (request.action == Action.Join) {
return gameManager.join();
@ -20,9 +21,15 @@ export function handler(requestBytes: Uint8Array): string {
} else if (request.action == Action.Unknown) {
let r = request as UnknownRequest;
let error = new ErrorResponse(r.message);
return error.serialize();
let returnStr = error.serialize();
memory.free(changetype<usize>(error));
return returnStr;
}
memory.free(changetype<usize>(request));
let response = new ErrorResponse("Unreachable.");
return response.serialize();
let returnStr = response.serialize();
memory.free(changetype<usize>(response));
return returnStr;
}

View File

@ -1,4 +1,5 @@
// import "allocator/tlsf";
// import "collector/itcm";
import "allocator/buddy";
// import "allocator/arena";
@ -35,6 +36,7 @@ export function invoke(ptr: i32, size: i32): i32 {
store<u8>(strAddr + i, b);
}
memory.free(changetype<usize>(result));
memory.free(ptr);
return addr;

View File

@ -10,6 +10,10 @@ export enum Action {
export abstract class Request {
public action: Action = null;
clear(): void {
}
}
export class UnknownRequest extends Request {
@ -21,6 +25,11 @@ export class UnknownRequest extends Request {
this.message = message;
}
clear(): void {
super.clear();
memory.free(changetype<usize>(this.message));
}
}
export class JoinRequest extends Request {
constructor() {
@ -39,6 +48,11 @@ export class RollRequest extends Request {
this.betSize = betSize;
this.action = Action.Roll;
}
clear(): void {
super.clear();
memory.free(changetype<usize>(this.playerId));
}
}
export class GetBalanceRequest extends Request {
public playerId: u64;
@ -61,15 +75,21 @@ export function decode(bytes: Uint8Array): Request {
return new UnknownRequest("'action' field is not specified.")
}
let request: Request;
if (action == "Join") {
return new JoinRequest();
request = new JoinRequest();
} else if (action == "Roll") {
return new RollRequest(jsonHandler.playerId, jsonHandler.betPlacement, jsonHandler.betSize)
request = new RollRequest(jsonHandler.playerId, jsonHandler.betPlacement, jsonHandler.betSize)
} else if (action == "GetBalance") {
return new GetBalanceRequest(jsonHandler.playerId)
request = new GetBalanceRequest(jsonHandler.playerId)
} else {
return new UnknownRequest("There is no request with action: " + action);
request = new UnknownRequest("There is no request with action: " + action);
}
jsonHandler.clean();
return request;
}
class RequestJSONEventsHandler extends JSONHandler {
@ -81,6 +101,10 @@ class RequestJSONEventsHandler extends JSONHandler {
public outcome: u8;
public playerBalance: u64;
clean(): void {
memory.free(changetype<usize>(this.action));
}
setString(name: string, value: string): void {
if (name == "action") {
this.action = value;

4
package-lock.json generated
View File

@ -634,8 +634,8 @@
}
},
"assemblyscript-json": {
"version": "github:nearprotocol/assemblyscript-json#1798ae8368daf449aae5f558be753dbe9d71c46c",
"from": "github:nearprotocol/assemblyscript-json"
"version": "github:fluencelabs/assemblyscript-json#df8e1e34259065ce3d8e72c98a96b429ab207630",
"from": "github:fluencelabs/assemblyscript-json"
},
"assign-symbols": {
"version": "1.0.0",

View File

@ -22,7 +22,7 @@
},
"dependencies": {
"bignum": "github:MaxGraey/bignum.wasm",
"assemblyscript-json": "github:nearprotocol/assemblyscript-json",
"assemblyscript-json": "github:fluencelabs/assemblyscript-json",
"typescript-collections": "github:fluencelabs/typescript-collections",
"crypto-ts": "github:hmoog/crypto-ts"
},