Add op_hello

This commit is contained in:
Gregory Schier
2023-02-17 15:36:54 -08:00
parent cf33a7d943
commit e69ccb62da
3 changed files with 15 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
use deno_ast::{MediaType, ParseParams, SourceTextInfo};
use deno_core::error::AnyError;
use deno_core::{Extension, JsRuntime, ModuleSource, ModuleType, RuntimeOptions};
use deno_core::{op, Extension, JsRuntime, ModuleSource, ModuleType, RuntimeOptions};
use std::rc::Rc;
use deno_core::futures::FutureExt;
@@ -11,7 +11,9 @@ pub fn run_plugin_sync(file_path: &str) -> Result<(), AnyError> {
}
pub async fn run_plugin(file_path: &str) -> Result<(), AnyError> {
let extension = Extension::builder("pluginjs").ops(vec![]).build();
let extension = Extension::builder("runtime")
.ops(vec![op_hello::decl()])
.build();
// Initialize a runtime instance
let mut runtime = JsRuntime::new(RuntimeOptions {
@@ -31,6 +33,13 @@ pub async fn run_plugin(file_path: &str) -> Result<(), AnyError> {
result.await?
}
#[op]
async fn op_hello(name: String) -> Result<String, AnyError> {
let contents = format!("Hello {} from Rust!", name);
println!("{}", contents);
Ok(contents)
}
struct TsModuleLoader;
impl deno_core::ModuleLoader for TsModuleLoader {

View File

@@ -1,4 +1,6 @@
(function (globalThis) {
Deno.core.initializeAsyncOps();
function argsToMessage(...args) {
return args.map((arg) => JSON.stringify(arg)).join(' ');
}