Skip to content

Commit

Permalink
Merge branch 'master' into dd/sails-gprimitives
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisInSky committed May 17, 2024
2 parents 28b591f + 51b731e commit 413e6f0
Show file tree
Hide file tree
Showing 35 changed files with 1,135 additions and 1,029 deletions.
719 changes: 382 additions & 337 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ license = "GPL-3.0"
resolver = "2"
members = [
"client-gen",
"examples/no-svcs-prog/app",
"examples/no-svcs-prog/wasm",
"examples/puppeteer/app",
"examples/puppeteer/wasm",
"examples/rmrk/catalog/app",
Expand Down
3 changes: 3 additions & 0 deletions examples/no-svcs-prog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# no-svcs-prog

Most basic program without services to get you started
7 changes: 7 additions & 0 deletions examples/no-svcs-prog/app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "no-svcs-prog-app"
version = "0.1.0"
edition = "2021"

[dependencies]
sails-rtl.workspace = true
13 changes: 13 additions & 0 deletions examples/no-svcs-prog/app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![no_std]

use sails_rtl::gstd::gprogram;

#[derive(Default)]
pub struct Program;

#[gprogram]
impl Program {
pub fn new() -> Self {
Self
}
}
13 changes: 13 additions & 0 deletions examples/no-svcs-prog/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "no-svcs-prog"
version = "0.1.0"
edition = "2021"

[dependencies]
no-svcs-prog-app = { path = "../app" }
sails-rtl.workspace = true

[build-dependencies]
gwasm-builder.workspace = true
no-svcs-prog-app = { path = "../app" }
sails-idl-gen.workspace = true
15 changes: 15 additions & 0 deletions examples/no-svcs-prog/wasm/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use no_svcs_prog_app::Program;
use sails_idl_gen::program;
use std::{env, fs::File, path::PathBuf};

fn main() {
gwasm_builder::build();

let manifest_dir_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());

let idl_file_path = manifest_dir_path.join("no-svcs-prog.idl");

let idl_file = File::create(idl_file_path).unwrap();

program::generate_idl::<Program>(idl_file).unwrap();
}
4 changes: 4 additions & 0 deletions examples/no-svcs-prog/wasm/no-svcs-prog.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
constructor {
New : ();
};

4 changes: 4 additions & 0 deletions examples/no-svcs-prog/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![no_std]

#[cfg(target_arch = "wasm32")]
pub use no_svcs_prog_app::wasm::*;
10 changes: 5 additions & 5 deletions examples/rmrk/resource/wasm/tests/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl<'a> Fixture<'a> {
})
}

fn __resource_program_for_sync(&'a self) -> &Program<'a> {
fn resource_program_for_sync(&'a self) -> &Program<'a> {
self.resource_program.get_or_init(|| {
tokio::runtime::Builder::new_current_thread()
.enable_all()
Expand All @@ -256,7 +256,7 @@ impl<'a> Fixture<'a> {
}

async fn __spin_up_program(&'a self, program_path: &str, payload: &[u8]) -> Program<'a> {
let code_id = self.program_space().system().submit_code(program_path);
let code_id = self.program_space().system().submit_code_file(program_path);
let program_space = self.program_space().clone();
let reply = program_space
.activate(
Expand All @@ -280,7 +280,7 @@ impl<'a> Fixture<'a> {
resource_id: ResourceId,
resource: &Resource,
) -> RunResult {
let program = self.resource_program_for_async();
let program = self.resource_program_for_sync();
let encoded_request = [
resources::RESOURCE_SERVICE_NAME.encode(),
resources::ADD_RESOURCE_ENTRY_FUNC_NAME.encode(),
Expand Down Expand Up @@ -322,7 +322,7 @@ impl<'a> Fixture<'a> {
resource_id: ResourceId,
part_id: PartId,
) -> RunResult {
let program = self.resource_program_for_async();
let program = self.resource_program_for_sync();
let encoded_request = [
resources::RESOURCE_SERVICE_NAME.encode(),
resources::ADD_PART_TO_RESOURCE_FUNC_NAME.encode(),
Expand All @@ -338,7 +338,7 @@ impl<'a> Fixture<'a> {
actor_id: u64,
resource_id: ResourceId,
) -> Option<ResourceStorageResult<Resource>> {
let program = self.resource_program_for_async();
let program = self.resource_program_for_sync();
let encoded_service_name = resources::RESOURCE_SERVICE_NAME.encode();
let encoded_func_name = resources::RESOURCE_FUNC_NAME.encode();
let encoded_request = [
Expand Down
2 changes: 0 additions & 2 deletions idl-gen/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ pub enum Error {
FuncMetaIsInvalid(String),
#[error("event meta is invalid: {0}")]
EventMetaIsInvalid(String),
#[error("at least one service is required")]
ServiceIsMissing,
#[error("type id `{0}` is not found in the type registry")]
TypeIdIsUnknown(u32),
#[error("type `{0}` is not supported")]
Expand Down
3 changes: 0 additions & 3 deletions idl-gen/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ impl ExpandedProgramMeta {
)
})
.collect::<Vec<_>>();
if services_data.is_empty() {
return Err(Error::ServiceIsMissing);
}
let registry = PortableRegistry::from(registry);
let ctors = Self::ctor_funcs(&registry, ctors_type_id)?;
let services = services_data
Expand Down
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sails-js",
"version": "0.1.3",
"version": "0.1.4",
"description": "Parser and typescript code generator from Sails IDL files",
"main": "lib/index.js",
"preferGlobal": true,
Expand Down
15 changes: 9 additions & 6 deletions js/src/generate/service-gen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getPayloadMethod, getJsTypeDef, getScaleCodecDef, toLowerCaseFirst } from '../utils/index.js';
import { FuncParam, Program, Service } from '../parser/visitor.js';
import { getPayloadMethod, getJsTypeDef, getScaleCodecDef, toLowerCaseFirst, PaylodMethod } from '../utils/index.js';
import { FuncParam, Service } from '../parser/service.js';
import { Program } from '../parser/program.js';
import { Output } from './output.js';

const HEX_STRING_TYPE = '`0x${string}`';
Expand Down Expand Up @@ -165,8 +166,9 @@ export class ServiceGenerator {

private generateMethods(service: Service) {
for (const { name, def, params, isQuery } of service.funcs) {
const returnType = getJsTypeDef(def);
const returnScaleType = getScaleCodecDef(def);
const decodeMethod = getPayloadMethod(returnScaleType);
const returnType = getJsTypeDef(def, decodeMethod);

this._out.line().block(getFuncSignature(name, params, returnType, isQuery), () => {
if (isQuery) {
Expand All @@ -186,7 +188,7 @@ export class ServiceGenerator {
.line(
`const result = this._program.registry.createType('(String, String, ${returnScaleType})', reply.payload)`,
)
.line(`return result[2].${getPayloadMethod(returnScaleType)}() as unknown as ${returnType}`);
.line(`return result[2].${decodeMethod}() as unknown as ${returnType}`);
} else {
this._out
.line(`if (!this._program.programId) throw new Error('Program ID is not set')`)
Expand Down Expand Up @@ -225,7 +227,8 @@ export class ServiceGenerator {
}

for (const event of service.events) {
const jsType = event.def ? getJsTypeDef(event.def) : 'null';
const decodeMethod = event.def ? getPayloadMethod(getScaleCodecDef(event.def)) : PaylodMethod.toJSON;
const jsType = event.def ? getJsTypeDef(event.def, decodeMethod) : 'null';

this._out
.line()
Expand Down Expand Up @@ -255,7 +258,7 @@ export class ServiceGenerator {
`callback(this._program.registry.createType('(String, String, ${getScaleCodecDef(
event.def,
true,
)})', message.payload)[2].toJSON() as ${jsType})`,
)})', message.payload)[2].${decodeMethod}() as ${jsType})`,
);
}
},
Expand Down
3 changes: 2 additions & 1 deletion js/src/generate/types-gen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EnumDef, EnumVariant, Program, TypeDef } from '../parser/index.js';
import { EnumDef, EnumVariant, TypeDef } from '../parser/types.js';
import { Program } from '../parser/program.js';
import { toLowerCaseFirst, getJsTypeDef } from '../utils/index.js';
import { Output } from './output.js';

Expand Down
4 changes: 3 additions & 1 deletion js/src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './parser.js';
export * from './visitor.js';
export { Program, Ctor, CtorFunc } from './program.js';
export { Service, ServiceFunc, ServiceEvent } from './service.js';
export { TypeDef } from './types.js';
11 changes: 3 additions & 8 deletions js/src/parser/parser.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import wasmParserBytes from './wasm-bytes.js';
import {
Ctor,
CtorFunc,
DefKind,
EnumDef,
EnumVariant,
FixedSizeArrayDef,
ServiceFunc,
FuncParam,
MapDef,
OptionalDef,
PrimitiveDef,
Program,
ResultDef,
Service,
StructDef,
StructField,
Type,
TypeDef,
UserDefinedDef,
VecDef,
ServiceEvent,
} from './visitor.js';
} from './types.js';
import { Ctor, CtorFunc, Program } from './program.js';
import { FuncParam, Service, ServiceEvent, ServiceFunc } from './service.js';

const WASM_PAGE_SIZE = 0x10000;

Expand Down
103 changes: 103 additions & 0 deletions js/src/parser/program.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { FuncParam, Service } from './service.js';
import { Type, WithDef } from './types.js';
import { getName } from './util.js';
import { Base } from './visitor.js';

export class Program {
private _services: Service[];
private _types: Map<number, Type>;
private _context: Map<number, WithDef>;
private _ctor: Ctor;

constructor() {
this._services = [];
this._types = new Map();
this._context = new Map();
}

addService(service: Service) {
this._services.push(service);
}

addType(type: Type) {
const id = type.rawPtr;
this._types.set(id, type);
this._context.set(id, type);
return id;
}

get services(): Service[] {
return this._services;
}

get ctor(): Ctor {
return this._ctor;
}

getType(id: number): Type {
return this._types.get(id);
}

getContext(id: number): any {
return this._context.get(id);
}

addContext(id: number, ctx: any) {
this._context.set(id, ctx);
}

get types(): Type[] {
return Array.from(this._types.values());
}

getTypeByName(name: string): Type {
const types = this.types.filter((type) => type.name === name);
if (types.length > 1) throw new Error(`multiple types found with name ${name}`);
if (types.length === 0) throw new Error(`no type found with name ${name}`);

return types[0];
}

addCtor(ctor: Ctor) {
this._ctor = ctor;
}
}

export class Ctor extends Base {
public readonly funcs: CtorFunc[];

constructor(ptr: number, memory: WebAssembly.Memory) {
super(ptr, memory);

this.funcs = [];
}

addFunc(func: CtorFunc) {
this.funcs.push(func);
}
}

export class CtorFunc extends Base {
private _params: Map<number, FuncParam>;
public readonly name: string;

constructor(ptr: number, memory: WebAssembly.Memory) {
super(ptr, memory);

const { name, offset } = getName(ptr, this.offset, memory);
this.name = name;
this.offset = offset;

this._params = new Map();
}

addFuncParam(ptr: number, param: FuncParam) {
this._params.set(ptr, param);
}

get params(): FuncParam[] {
if (this._params.size === 0) return [];

return Array.from(this._params.values());
}
}
Loading

0 comments on commit 413e6f0

Please sign in to comment.