Skip to content

Commit

Permalink
Latest updates
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Moore <[email protected]>
  • Loading branch information
jimmyaxod committed Sep 29, 2023
1 parent 184b2cf commit 5d37275
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 23 deletions.
5 changes: 3 additions & 2 deletions extension/generator/golang/templates/host.go.templ
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ type {{ .schema.Name }}Host struct {
{{ range $fn := .schema.Functions }}

func (h *{{ $schema.Name }}Host) host_ext_{{ $schema.Name }}_{{ $fn.Name}}(mem extension.ModuleMemory, resize extension.Resizer, params []uint64) {
ptr := uint32(params[0])
length := uint32(params[1])

ptr := uint32(params[1])
length := uint32(params[2])
data, _ := mem.Read(ptr, length)

cd := &{{ $fn.Params }}{}
Expand Down
10 changes: 10 additions & 0 deletions extension/generator/rust/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func (g *Generator) GenerateGuest(extensionSchema *extension.Schema, extensionHa

func templateFunctions() template.FuncMap {
return template.FuncMap{
"IsInterface": isInterface,
"Primitive": primitive,
"IsPrimitive": extension.ValidPrimitiveType,
"PolyglotPrimitive": polyglotPrimitive,
Expand All @@ -188,6 +189,15 @@ func templateFunctions() template.FuncMap {
}
}

func isInterface(schema *extension.Schema, s string) bool {
for _, i := range schema.Interfaces {
if i.Name == s {
return true
}
}
return false
}

func primitive(t string) string {
switch t {
case "string":
Expand Down
67 changes: 56 additions & 11 deletions extension/generator/rust/guest.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Code generated by scale-extension 0.4.1, DO NOT EDIT.
// output: guest

pub mod HttpFetch;



pub mod types;
use crate::types::{Encode, Decode};

use std::io::Cursor;
Expand All @@ -12,6 +15,23 @@ static HASH: &'static str = "3914ee157703d809e20bf4e9f4a6d0cf0db287ec4b3dcfe4982
static mut READ_BUFFER: Vec<u8> = Vec::new();
static mut WRITE_BUFFER: Vec<u8> = Vec::new();

// Interfaces



// Interface for HttpConnector

pub trait HttpConnector {


fn Fetch(&self, ConnectionDetails) -> Result<Option<types::HttpResponse>, Box<dyn std::error::Error>>



}



// resize resizes the extensions READ_BUFFER to the given size and returns the pointer to the buffer
//
// Users should not use this method.
Expand All @@ -22,18 +42,17 @@ pub unsafe fn ext_HttpFetch_Resize(size: u32) -> *const u8 {
return READ_BUFFER.as_ptr();
}



// Define any interfaces we need here...
// Also define structs we can use to hold instanceId



// Define concrete types with a hidden instanceId HttpConnector

// type _HttpConnector struct {
// instanceId uint64
// }
#[derive(Clone, Debug, PartialEq)]
pub struct _HttpConnector {
pub instanceId: u64,
}


// func (d *_HttpConnector) Fetch(params *ConnectionDetails) (HttpResponse, error) {
Expand All @@ -51,12 +70,38 @@ pub unsafe fn ext_HttpFetch_Resize(size: u32) -> *const u8 {



//export ext_HttpFetch_New
//go:linkname ext_HttpFetch_New
//func ext_HttpFetch_New(instance uint64, offset uint32, length uint32) uint64
#[link(wasm_import_module = "env")]
extern "C" {
#[link_name = "ext_HttpFetch_New"]
fn _ext_HttpFetch_New(instance: u64, ptr: u32, size: u32) -> u64;
}
pub fn New(params: types::HttpConfig) -> Result<Option<impl types::HttpConnector>, Box<dyn std::error::Error>> {

// TODO: First we take the params, serialize them.
//writeBuffer.Reset()
//params.Encode(writeBuffer)
//underlying := writeBuffer.Bytes()
//ptr := &underlying[0]
//unsafePtr := uintptr(unsafe.Pointer(ptr))
//off := uint32(unsafePtr)
//l := uint32(writeBuffer.Len())

// Now make the call to the host.

let off = WRITE_BUFFER.as_ptr() as u32;
let l = WRITE_BUFFER.len() as u32;
let v = _ext_HttpFetch_New(0, off, l);
// IF the return type is an interface return ifc, which contains hidden instanceId.

// TODO: Handle error from host. In this case there'll be an error in the readBuffer

//func New(params *HttpConfig) (HttpConnector, error) {
//}
let c = _HttpConnector{
instanceId: v,
};

return Ok(Some(c));

}



Expand Down
101 changes: 91 additions & 10 deletions extension/generator/rust/templates/guest.rs.templ
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

{{ $schema := .extension_schema }}

pub mod types;
use crate::types::{Encode, Decode};

Expand All @@ -9,6 +12,27 @@ static HASH: &'static str = "{{ .extension_hash }}";
static mut READ_BUFFER: Vec<u8> = Vec::new();
static mut WRITE_BUFFER: Vec<u8> = Vec::new();
// Interfaces
{{ range $ifc := .extension_schema.Interfaces }}
// Interface for {{ $ifc.Name }}
pub trait {{ $ifc.Name }} {
{{ range $fn := $ifc.Functions }}
{{- if (IsInterface $schema $fn.Return) }}
fn {{ $fn.Name }}(&self, params: types::{{ $fn.Params }}) -> Result<Option<impl types::{{ $fn.Return }}>, Box<dyn std::error::Error>>;
{{ else }}
fn {{ $fn.Name }}(&self, params: types::{{ $fn.Params }}) -> Result<Option<types::{{ $fn.Return }}>, Box<dyn std::error::Error>>;
{{ end }}
{{ end }}
}
{{ end }}
// resize resizes the extensions READ_BUFFER to the given size and returns the pointer to the buffer
//
// Users should not use this method.
Expand All @@ -19,18 +43,19 @@ pub unsafe fn ext_{{ .extension_schema.Name }}_Resize(size: u32) -> *const u8 {
return READ_BUFFER.as_ptr();
}
{{ $schema := .extension_schema }}
// Define any interfaces we need here...
// Also define structs we can use to hold instanceId
{{ range $ifc := .extension_schema.Interfaces }}
// Define concrete types with a hidden instanceId {{ $ifc.Name }}
// type _{{ $ifc.Name }} struct {
// instanceId uint64
// }
#[derive(Clone, Debug, PartialEq)]
pub struct _{{ $ifc.Name }} {
pub instanceId: u64,
}
impl {{ $ifc.Name }} for _{{ $ifc.Name }} {
{{ range $fn := $ifc.Functions }}
// func (d *_{{ $ifc.Name }}) {{ $fn.Name }}(params *{{ $fn.Params }}) ({{ $fn.Return }}, error) {
Expand All @@ -40,20 +65,76 @@ pub unsafe fn ext_{{ .extension_schema.Name }}_Resize(size: u32) -> *const u8 {
//go:linkname ext_{{ $schema.Name }}_{{ $ifc.Name }}_{{ $fn.Name }}
//func ext_{{ $schema.Name }}_{{ $ifc.Name }}_{{ $fn.Name }}(instance uint64, offset uint32, length uint32) uint64
{{- if (IsInterface $schema $fn.Return) }}
fn {{ $fn.Name }}(&self, params: types::{{ $fn.Params }}) -> Result<Option<impl {{ $fn.Return }}>, Box<dyn std::error::Error>> {
{{ else }}
fn {{ $fn.Name }}(&self, params: types::{{ $fn.Params }}) -> Result<Option<types::{{ $fn.Return }}>, Box<dyn std::error::Error>> {
{{ end }}
// TODO: Function body goes here...
return Ok(None);
}
{{ end }}
}
{{ end }}
// Define any global functions here...
{{ range $fn := .extension_schema.Functions }}
//export ext_{{ $schema.Name }}_{{ $fn.Name }}
//go:linkname ext_{{ $schema.Name }}_{{ $fn.Name }}
//func ext_{{ $schema.Name }}_{{ $fn.Name }}(instance uint64, offset uint32, length uint32) uint64
#[link(wasm_import_module = "env")]
extern "C" {
#[link_name = "ext_{{ $schema.Name }}_{{ $fn.Name }}"]
fn _ext_{{ $schema.Name }}_{{ $fn.Name }}(instance: u64, ptr: u32, size: u32) -> u64;
}
{{- if (IsInterface $schema $fn.Return) }}
pub fn {{ $fn.Name }}(params: types::{{ $fn.Params }}) -> Result<Option<impl {{ $fn.Return }}>, Box<dyn std::error::Error>> {
{{ else }}
pub fn {{ $fn.Name }}(params: types::{{ $fn.Params }}) -> Result<Option<types::{{ $fn.Return }}>, Box<dyn std::error::Error>> {
{{ end }}
unsafe {
let mut cursor = Cursor::new(Vec::new());
//func {{ $fn.Name }}(params *{{ $fn.Params }}) ({{ $fn.Return }}, error) {
//}
types::{{ $fn.Params }}::encode(Some(&params), &mut cursor);
let vec = cursor.into_inner();
WRITE_BUFFER.resize(vec.len() as usize, 0);
WRITE_BUFFER.copy_from_slice(&vec);
// Now make the call to the host.
let mut off = WRITE_BUFFER.as_ptr() as u32;
let mut l = WRITE_BUFFER.len() as u32;
{{- if (IsInterface $schema $fn.Return) }}
let v = _ext_{{ $schema.Name }}_{{ $fn.Name }}(0, off, l);
// IF the return type is an interface return ifc, which contains hidden instanceId.
// TODO: Handle error from host. In this case there'll be an error in the readBuffer

let c = _{{ $fn.Return }}{
instanceId: v,
};

return Ok(Some(c));
{{ else }}
_ext_{{ $schema.Name }}_{{ $fn.Name }}(0, off, l);
// IF the return type is a model, we should read the data from the read buffer.

let c = {{ $fn.Return }}{}
// TODO: Do the decode
//r, err := Decode{{ $fn.Return }}(ret, readBuffer)
return Ok(Some(c))
{{ end }}
}
}

{{ end }}

Expand Down

0 comments on commit 5d37275

Please sign in to comment.