Skip to content

Commit

Permalink
fix tests regarding latest changes from origin
Browse files Browse the repository at this point in the history
  • Loading branch information
ramilexe committed Aug 23, 2022
1 parent 99a31ac commit 6d637e4
Show file tree
Hide file tree
Showing 53 changed files with 399 additions and 73 deletions.
1 change: 1 addition & 0 deletions packages/schema/bind/src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function fetchTestCases(): TestCases {
const outputLanguages = fs
.readdirSync(outputDir, { withFileTypes: true })
.filter((item: fs.Dirent) => item.isDirectory())
.filter((item: fs.Dirent) => item.name == "wasm-go")
.map((item: fs.Dirent) => {
return {
language: item.name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { reservedWordsAS } from "./reservedWords";
import { MustacheFn } from "../../types";
import { MustacheFn } from "../types";

let num = -1;

Expand Down
3 changes: 3 additions & 0 deletions packages/schema/bind/src/bindings/golang/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * as Wasm from "./wasm";
export * as Functions from "./functions";
export * as Types from "./types";
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const baseTypes = {
const types = {
u8: "u8",
u16: "u16",
u32: "u32",
Expand All @@ -9,10 +9,10 @@ const baseTypes = {
bool: "bool",
};

export type BaseTypes = typeof baseTypes;
export type BaseTypes = typeof types;

export type BaseType = keyof BaseTypes;

export function isBaseType(type: string): type is BaseType {
return type in baseTypes;
return type in types;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Functions from "./functions";
import { Functions } from "../";
import { GenerateBindingFn } from "../..";
import { renderTemplates, loadSubTemplates } from "../../utils/templates";
import { renderTemplates } from "../..";
import { loadSubTemplates } from "../../utils";
import { BindOptions, BindOutput } from "../../..";

import {
Expand Down Expand Up @@ -37,71 +38,81 @@ export const generateBinding: GenerateBindingFn = (
const abi = applyTransforms(options.abi);

// Generate object type folders
for (const objectType of abi.objectTypes) {
output.entries.push({
type: "Directory",
name: "types",
data: renderTemplates(
templatePath("object-type"),
objectType,
subTemplates
),
});
if (abi.objectTypes) {
for (const objectType of abi.objectTypes) {
output.entries.push({
type: "Directory",
name: "types",
data: renderTemplates(
templatePath("object-type"),
objectType,
subTemplates
),
});
}
}

// Generate imported folder
const importEntries: OutputEntry[] = [];

// Generate imported module type folders
for (const importedModuleType of abi.importedModuleTypes) {
importEntries.push({
type: "Directory",
name: `${camel2snake(importedModuleType.namespace)}`,
data: renderTemplates(
templatePath("imported/module-type"),
importedModuleType,
subTemplates
),
});
if (abi.importedModuleTypes) {
for (const importedModuleType of abi.importedModuleTypes) {
importEntries.push({
type: "Directory",
name: `${camel2snake(importedModuleType.namespace)}`,
data: renderTemplates(
templatePath("imported/module-type"),
importedModuleType,
subTemplates
),
});
}
}

// // Generate imported env type folders
for (const importedEnvType of abi.importedEnvTypes) {
importEntries.push({
type: "Directory",
name: `${camel2snake(importedEnvType.namespace)}`,
data: renderTemplates(
templatePath("imported/env-type"),
importedEnvType,
subTemplates
),
});
if (abi.importedEnvTypes) {
for (const importedEnvType of abi.importedEnvTypes) {
importEntries.push({
type: "Directory",
name: `${camel2snake(importedEnvType.namespace)}`,
data: renderTemplates(
templatePath("imported/env-type"),
importedEnvType,
subTemplates
),
});
}
}

// Generate imported enum type folders
for (const importedEnumType of abi.importedEnumTypes) {
importEntries.push({
type: "Directory",
name: `${camel2snake(importedEnumType.namespace)}`,
data: renderTemplates(
templatePath("imported/enum-type"),
importedEnumType,
subTemplates
),
});
if (abi.importedEnumTypes) {
for (const importedEnumType of abi.importedEnumTypes) {
importEntries.push({
type: "Directory",
name: `${camel2snake(importedEnumType.namespace)}`,
data: renderTemplates(
templatePath("imported/enum-type"),
importedEnumType,
subTemplates
),
});
}
}

// Generate imported object type folders
for (const importedObectType of abi.importedObjectTypes) {
importEntries.push({
type: "Directory",
name: `${camel2snake(importedObectType.namespace)}`,
data: renderTemplates(
templatePath("imported/object-type"),
importedObectType,
subTemplates
),
});
if (abi.importedObjectTypes) {
for (const importedObectType of abi.importedObjectTypes) {
importEntries.push({
type: "Directory",
name: `${camel2snake(importedObectType.namespace)}`,
data: renderTemplates(
templatePath("imported/object-type"),
importedObectType,
subTemplates
),
});
}
}

if (importEntries.length) {
Expand All @@ -116,16 +127,18 @@ export const generateBinding: GenerateBindingFn = (
}

// Generate interface type folders
for (const interfaceType of abi.interfaceTypes) {
output.entries.push({
type: "Directory",
name: "interfaces",
data: renderTemplates(
templatePath("interface-type"),
interfaceType,
subTemplates
),
});
if (abi.interfaceTypes) {
for (const interfaceType of abi.interfaceTypes) {
output.entries.push({
type: "Directory",
name: "interfaces",
data: renderTemplates(
templatePath("interface-type"),
interfaceType,
subTemplates
),
});
}
}

// Generate module type folders
Expand All @@ -142,12 +155,18 @@ export const generateBinding: GenerateBindingFn = (
}

// Generate enum type folders
for (const enumType of abi.enumTypes) {
output.entries.push({
type: "Directory",
name: "types",
data: renderTemplates(templatePath("enum-type"), enumType, subTemplates),
});
if (abi.enumTypes) {
for (const enumType of abi.enumTypes) {
output.entries.push({
type: "Directory",
name: "types",
data: renderTemplates(
templatePath("enum-type"),
enumType,
subTemplates
),
});
}
}

// Generate env type folders
Expand Down
2 changes: 2 additions & 0 deletions packages/test-cases/cases/bind/sanity/output/wasm-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func _wrap_invoke(methodSize, argsSize, envSize uint32) bool {
return polywrap.WrapInvoke(args, envSize, module.ObjectMethodWrapped)
case "optionalEnvMethod":
return polywrap.WrapInvoke(args, envSize, module.OptionalEnvMethodWrapped)
case "if":
return polywrap.WrapInvoke(args, envSize, module.IfWrapped)
default:
return polywrap.WrapInvoke(args, envSize, nil)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package types

type While int32

const (
Whilefor = iota
Whilein = iota
whileMax = iota
)

func SanitizeWhileValue(value int32) {
if !(value >= 0 && value < int32(whileMax)) {
panic("Invalid value for enum 'While'")
}
}

func GetWhileValue(key string) While {
switch key {
case "for":
return Whilefor
case "in":
return Whilein
default:
panic("Invalid key for enum 'While'")
}
}

func GetWhileKey(value While) string {
SanitizeWhileValue(int32(value))
switch value {
case Whilefor:
return "for"
case Whilein:
return "in"
default:
panic("Invalid value for enum 'While'")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,56 @@ func WriteOptionalEnvMethodResult(writer msgpack.Write, value *AnotherType) {
}
writer.Context().Pop()
}

type ArgsIf struct {
M_if else
}

func DeserializeIfArgs(argsBuf []byte) *ArgsIf {
ctx := msgpack.NewContext("Deserializing module-type: If")
reader := msgpack.NewReadDecoder(ctx, argsBuf)

var (
_if else
_ifSet bool
)

for i := int32(reader.ReadMapLength()); i > 0; i-- {
field := reader.ReadString()
reader.Context().Push(field, "unknown", "searching for property type")
reader.Context().Pop()
switch field {
case "M_if":
reader.Context().Push(field, "else", "type found, reading property")
if v := ElseRead(reader); v != nil {
_if = *v
}
_ifSet = true
reader.Context().Pop()
}
}

if !_ifSet {
panic(reader.Context().PrintWithContext("Missing required property: 'if: else'"))
}

return &ArgsIf{
M_if: _if,
}
}

func SerializeIfResult(value else) []byte {
ctx := msgpack.NewContext("Serializing module-type: If")
encoder := msgpack.NewWriteEncoder(ctx)
WriteIfResult(encoder, value);
return encoder.Buffer()
}

func WriteIfResult(writer msgpack.Write, value else) {
writer.Context().Push("if", "else", "writing property")
{
v := value
ElseWrite(writer, &v)
}
writer.Context().Pop()
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ func OptionalEnvMethodWrapped(argsBuf []byte, envSize uint32) []byte {
return SerializeOptionalEnvMethodResult(result)
}

func IfWrapped(argsBuf []byte, envSize uint32) []byte {
var env *Env

args := DeserializeIfArgs(argsBuf)

result := methods.If(args)
return SerializeIfResult(result)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package types

import (
"github.com/consideritdone/polywrap-go/polywrap/msgpack"
)

type CustomMapValue struct {
Foo string
}

func CustomMapValueToBuffer(value *CustomMapValue) []byte {
return serializeCustomMapValue(value)
}

func CustomMapValueFromBuffer(data []byte) *CustomMapValue {
return deserializeCustomMapValue(data)
}

func CustomMapValueWrite(writer msgpack.Write, value *CustomMapValue) {
writeCustomMapValue(writer, value)
}

func CustomMapValueRead(reader msgpack.Read) *CustomMapValue {
return readCustomMapValue(reader)
}
Loading

0 comments on commit 6d637e4

Please sign in to comment.