Skip to content

Commit

Permalink
Use unescaped template operator (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhelx authored Aug 21, 2024
1 parent 1a76382 commit daeb5f7
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 48 deletions.
8 changes: 4 additions & 4 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ So `src/index.d.ts.ejs` will become `src/index.d.ts`. It might start off like th

declare module 'main' {
<% ctx.schema.exports.forEach(e => { %>
export function <% toCamelCase(e.name) %>(): I32;
<%- } %>
export function <%- toCamelCase(e.name) %>(): I32;
<% } %>
}

<% if (ctx.schema.imports.length > 0) { %>
declare module 'extism:host' {
interface user {
<% ctx.schema.import.forEach(i => { %>
<% toCamelCase(i.name) %>(ptr: I64): I64;
<%- end %>
<%- toCamelCase(i.name) %>(ptr: I64): I64;
<% } %>
}
}
<% } %>
Expand Down
4 changes: 2 additions & 2 deletions template/package.json.ejs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "<%= project.name %>",
"name": "<%- project.name %>",
"version": "1.0.0",
"description": "<%= project.description %>",
"description": "<%- project.description %>",
"main": "src/index.ts",
"scripts": {
"build": "npx tsc --noEmit && node esbuild.js && extism-js dist/index.js -i src/index.d.ts -o dist/plugin.wasm",
Expand Down
4 changes: 2 additions & 2 deletions template/src/index.d.ts.ejs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
declare module 'main' {
<% schema.exports.forEach(ex => { -%>
export function <%= ex.name %>(): I32;
export function <%- ex.name %>(): I32;
<% }) -%>
}
<% if (schema.imports.length > 0) { -%>
declare module 'extism:host' {
interface user {
<% schema.imports.forEach(im => { -%>
<%= im.name %>(ptr: I64): I64;
<%- im.name %>(ptr: I64): I64;
<% }) -%>
}
}
Expand Down
18 changes: 9 additions & 9 deletions template/src/index.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@ import * as main from "./main"
<% if (Object.values(schema.schemas).length > 0) { %>
import {
<% Object.values(schema.schemas).forEach(schema => { -%>
<%= schema.name %>,
<%- schema.name %>,
<% }) -%>
} from './pdk'
<% } %>

<% schema.exports.forEach(ex => { -%>
export function <%= ex.name %>(): number {
export function <%- ex.name %>(): number {
<% if (ex.input) { -%>
<% if (isJsonEncoded(ex.input)) { -%>
<% if (isPrimitive(ex.input)) { -%>
const input: <%- toTypeScriptType(ex.input) %> = JSON.parse(Host.inputString())
<% } else { -%>
const untypedInput = JSON.parse(Host.inputString())
const input = <%= toTypeScriptType(ex.input) %>.fromJson(untypedInput)
const input = <%- toTypeScriptType(ex.input) %>.fromJson(untypedInput)
<% } -%>
<% } else if (ex.input.type === 'string') { -%>
const input = Host.inputString() <%= (ex.input.$ref && ex.input.$ref.enum) ? `as ${ex.input.$ref.name}` : "" %>
const input = Host.inputString() <%- (ex.input.$ref && ex.input.$ref.enum) ? `as ${ex.input.$ref.name}` : "" %>
<% } else { -%>
const input: ArrayBufferLike = Host.inputBytes()
<% } -%>
<% if (ex.output) { -%>
const output = main.<%= ex.name %>Impl(input)
const output = main.<%- ex.name %>Impl(input)
<% } else { -%>
main.<%= ex.name %>Impl(input)
main.<%- ex.name %>Impl(input)
<% } -%>
<% } else { -%>
<% if (ex.output) { -%>
const output = main.<%= ex.name %>Impl()
const output = main.<%- ex.name %>Impl()
<% } else { -%>
main.<%= ex.name %>Impl()
main.<%- ex.name %>Impl()
<% } -%>
<% } -%>
Expand All @@ -41,7 +41,7 @@ export function <%= ex.name %>(): number {
<% if (isPrimitive(ex.output)) { -%>
Host.outputString(JSON.stringify(output))
<% } else { -%>
const untypedOutput = <%= toTypeScriptType(ex.output) %>.toJson(output)
const untypedOutput = <%- toTypeScriptType(ex.output) %>.toJson(output)
Host.outputString(JSON.stringify(untypedOutput))
<% } -%>
<% } else if (ex.output.type === 'string') { -%>
Expand Down
12 changes: 6 additions & 6 deletions template/src/main.ts.ejs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<% if (Object.values(schema.schemas).length > 0) { %>
import {
<% Object.values(schema.schemas).forEach(schema => { -%>
<%= schema.name %>,
<%- schema.name %>,
<% }) -%>
} from './pdk'
<% } %>
<% if (schema.imports.length > 0) { %>
import {
<% schema.imports.forEach(imp => { -%>
<%= imp.name %>,
<%- imp.name %>,
<% }) -%>
} from './pdk'
<% } %>
Expand All @@ -17,17 +17,17 @@ import {
<% schema.exports.forEach(ex => { %>
<% if (hasComment(ex)) -%>
/**
* <%= formatCommentBlock(ex.description) %>
* <%- formatCommentBlock(ex.description) %>
*
<% if (hasComment(ex.input)) { -%>
* @param input {<%- toTypeScriptType(ex.input) %>} <%= formatCommentLine(ex.input.description) %>
* @param input {<%- toTypeScriptType(ex.input) %>} <%- formatCommentLine(ex.input.description) %>
<% } -%>
<% if (hasComment(ex.output)) { -%>
* @returns {<%- toTypeScriptType(ex.output) %>} <%= formatCommentLine(ex.output.description) %>
* @returns {<%- toTypeScriptType(ex.output) %>} <%- formatCommentLine(ex.output.description) %>
<% } -%>
*/
<% -%>
export function <%= ex.name %>Impl(<%- ex.input ? `input: ${toTypeScriptType(ex.input)}`: null %>)<%- ex.output ? `:${toTypeScriptType(ex.output)}` : null %> {
export function <%- ex.name %>Impl(<%- ex.input ? `input: ${toTypeScriptType(ex.input)}`: null %>)<%- ex.output ? `:${toTypeScriptType(ex.output)}` : null %> {
<% if (featureFlags['stub-with-code-samples'] && codeSamples(ex, 'typescript').length > 0) { -%>
<%- codeSamples(ex, 'typescript')[0].source %>
<% } else { -%>
Expand Down
44 changes: 22 additions & 22 deletions template/src/pdk.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,44 @@ const hostFunctions = Host.getFunctions()
<% if (schema.properties.length > 0) { %>
/**
* <%= formatCommentBlock(schema.description) %>
* <%- formatCommentBlock(schema.description) %>
*/
export class <%= schema.name %> {
export class <%- schema.name %> {
<% schema.properties.forEach(p => { %>
<% if (p.description) { %>
/**
* <%= formatCommentBlock(p.description) %>
* <%- formatCommentBlock(p.description) %>
*/
<% } -%>
<%= (p.nullable || toTypeScriptType(p) === 'any') ? null : '// @ts-expect-error TS2564\n' -%>
<%= p.name %><%= p.nullable ? '?' : null %>: <%- toTypeScriptType(p) %>;
<%- (p.nullable || toTypeScriptType(p) === 'any') ? null : '// @ts-expect-error TS2564\n' -%>
<%- p.name %><%- p.nullable ? '?' : null %>: <%- toTypeScriptType(p) %>;
<% }) %>
static fromJson(obj: any): <%= schema.name %> {
static fromJson(obj: any): <%- schema.name %> {
return {
...obj,
<% schema.properties.forEach(p => { -%>
<% if (needsCasting(p)) { -%>
<% if (isDateTime(p)) { -%>
<%= p.name -%>: obj.<%= p.name -%> ? new Date(obj.<%= p.name -%>) : null,
<%- p.name -%>: obj.<%- p.name -%> ? new Date(obj.<%- p.name -%>) : null,
<% } else if (!isPrimitive(p)) {-%>
<%= p.name -%>: obj.<%= p.name -%> ? <%= p.$ref.name %>.fromJson(obj.<%= p.name %>) : null,
<%- p.name -%>: obj.<%- p.name -%> ? <%- p.$ref.name %>.fromJson(obj.<%- p.name %>) : null,
<% } -%>
<% } -%>
<% }) -%>
}
}
static toJson(obj: <%= schema.name %>): any{
static toJson(obj: <%- schema.name %>): any{
return {
...obj,
<% schema.properties.forEach(p => { -%>
<% if (needsCasting(p)) { -%>
<% if (p.type === "string" && p.format === "date-time") { -%>
<%= p.name -%>: obj.<%= p.name -%> ? obj.<%= p.name %>.toISOString() : null,
<%- p.name -%>: obj.<%- p.name -%> ? obj.<%- p.name %>.toISOString() : null,
<% } else if (p.$ref && !p.$ref.enum) {-%>
<%= p.name -%>: obj.<%= p.name -%> ? <%= p.$ref.name %>.toJson(obj.<%= p.name %>) : null,
<%- p.name -%>: obj.<%- p.name -%> ? <%- p.$ref.name %>.toJson(obj.<%- p.name %>) : null,
<% } -%>
<% } -%>
<% }) -%>
Expand All @@ -53,11 +53,11 @@ export class <%= schema.name %> {
<% } else if (schema.enum) { %>
/**
* <%= formatCommentLine(schema.description) %>
* <%- formatCommentLine(schema.description) %>
*/
export enum <%= schema.name %> {
export enum <%- schema.name %> {
<% schema.enum.forEach(variant => { -%>
<%= variant %> = "<%= variant %>",
<%- variant %> = "<%- variant %>",
<% }) -%>
}
Expand All @@ -68,22 +68,22 @@ export enum <%= schema.name %> {
<% schema.imports.forEach(imp => { %>
<% if (hasComment(imp)) -%>
/**
* <%= formatCommentBlock(imp.description) %>
* <%- formatCommentBlock(imp.description) %>
*
<% if (hasComment(imp.input)) { -%>
* @param input {<%- toTypeScriptType(imp.input) %>} <%= formatCommentLine(imp.input.description) %>
* @param input {<%- toTypeScriptType(imp.input) %>} <%- formatCommentLine(imp.input.description) %>
<% } -%>
<% if (hasComment(imp.output)) { -%>
* @returns {<%- toTypeScriptType(imp.output) %>} <%= formatCommentLine(imp.output.description) %>
* @returns {<%- toTypeScriptType(imp.output) %>} <%- formatCommentLine(imp.output.description) %>
<% } -%>
*/
export function <%= imp.name %>(input:<%- imp.input ? toTypeScriptType(imp.input) : 'any' %>) <%- imp.output ? `:${toTypeScriptType(imp.output)}` : null %> {
export function <%- imp.name %>(input:<%- imp.input ? toTypeScriptType(imp.input) : 'any' %>) <%- imp.output ? `:${toTypeScriptType(imp.output)}` : null %> {
<% if (imp.input) { -%>
<% if (isJsonEncoded(imp.input)) { -%>
<% if (isPrimitive(imp.input)) { %>
const mem = Memory.fromJsonObject(input as any)
<% } else { %>
const casted = <%= toTypeScriptType(imp.input) %>.toJson(input)
const casted = <%- toTypeScriptType(imp.input) %>.toJson(input)
const mem = Memory.fromJsonObject(casted)
<% } %>
<% } else if (isUtf8Encoded(imp.input)) { -%>
Expand All @@ -94,9 +94,9 @@ export function <%= imp.name %>(input:<%- imp.input ? toTypeScriptType(imp.input
const mem = Memory.fromBuffer(input)
<% } -%>
<% if (imp.output) { -%>const ptr =<% } -%> hostFunctions.<%= imp.name %>(mem.offset)
<% if (imp.output) { -%>const ptr =<% } -%> hostFunctions.<%- imp.name %>(mem.offset)
<% } else { -%>
<% if (imp.output) { -%>const ptr =<% } -%> hostFunctions.<%= imp.name %>()
<% if (imp.output) { -%>const ptr =<% } -%> hostFunctions.<%- imp.name %>()
<% } -%>
<% if (imp.output) { -%>
Expand All @@ -105,7 +105,7 @@ export function <%= imp.name %>(input:<%- imp.input ? toTypeScriptType(imp.input
return Memory.find(ptr).readJsonObject();
<% } else { -%>
const output = Memory.find(ptr).readJsonObject();
return <%= toTypeScriptType(imp.output) %>.fromJson(output)
return <%- toTypeScriptType(imp.output) %>.fromJson(output)
<% } -%>
<% } else if (isUtf8Encoded(imp.output)) { -%>
return Memory.find(ptr).readString();
Expand Down
6 changes: 3 additions & 3 deletions template/xtp.toml.ejs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
app_id = "<%= project.appId %>"
app_id = "<%- project.appId %>"

# This is where 'xtp plugin push' expects to find the wasm file after the build script has run.
bin = "dist/plugin.wasm"
extension_point_id = "<%= project.extensionPointId %>"
extension_point_id = "<%- project.extensionPointId %>"

# This is the 'binding' name used for the plugin.
name = "<%= project.name %>"
name = "<%- project.name %>"

[scripts]
# xtp plugin build runs this script to generate the wasm file
Expand Down

0 comments on commit daeb5f7

Please sign in to comment.