Skip to content

Commit 5077c21

Browse files
committed
First release of sheetruby, a spreadsheet application powered by mrubyedge.
1 parent a9868b6 commit 5077c21

File tree

6 files changed

+123
-33
lines changed

6 files changed

+123
-33
lines changed

.cargo/config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ rustflags = [
33
"-C",
44
"link-args=-o out.js",
55
"-C",
6-
"link-args=-s EXPORTED_FUNCTIONS=['_eval_ruby_script_returning_json','_eval_ruby_script_returning_json1','_main']",
6+
"link-args=-s EXPORTED_FUNCTIONS=['_eval_ruby_script_returning_json','_eval_ruby_script_returning_json1','_eval_ruby_script_returning_json2','_eval_ruby_script_returning_json3','_main']",
77
"-C",
88
"link-args=-s EXPORTED_RUNTIME_METHODS=['ccall','cwrap','UTF8ToString']",
99
"-C",

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sheetruby"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition = "2024"
55

66
[dependencies]
@@ -10,7 +10,6 @@ mrubyedge-math = "0.1.0"
1010
mrubyedge-serde-json = "0.1.0"
1111

1212
[profile.dev]
13-
# FIXME: unccomment
1413
# panic = "abort" # Reduce panic handling overhead
1514

1615
[profile.release]

combined.js

Lines changed: 20 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,43 @@
11
/**
22
* eval ruby script and return integer value
3-
* Arguments are accessible as $arg1, $arg2, etc. in Ruby code.
3+
* Arguments are accessible as $arg1, $arg2, $arg3 in Ruby code.
44
* @param {string} text ruby code.
5-
* @param {...(string|number|Array<Array<string|number>>)} args arguments accessible as $arg1, $arg2, etc. (converted to string).
6-
* @return The integer result of the Ruby script.
5+
* @param {...(string|number|Array<Array<string|number>>)} args arguments accessible as $arg1, $arg2, $arg3 (up to 3 arguments).
6+
* @return The result of the Ruby script (type inferred from JSON).
77
* @customfunction
88
*/
99
function EVAL_RUBY_SCRIPT(text) {
1010
var args = Array.prototype.slice.call(arguments, 1);
11+
var result;
1112
if (args.length === 0) {
12-
const result = Module.ccall(
13+
result = Module.ccall(
1314
'eval_ruby_script_returning_json',
1415
'string',
1516
['string'],
1617
[text]
1718
);
18-
return JSON.parse(result);
1919
} else if (args.length === 1) {
20-
const result = Module.ccall(
20+
result = Module.ccall(
2121
'eval_ruby_script_returning_json1',
2222
'string',
2323
['string', 'string'],
2424
[text, JSON.stringify(args[0])]
2525
);
26-
return JSON.parse(result);
26+
} else if (args.length === 2) {
27+
result = Module.ccall(
28+
'eval_ruby_script_returning_json2',
29+
'string',
30+
['string', 'string', 'string'],
31+
[text, JSON.stringify(args[0]), JSON.stringify(args[1])]
32+
);
2733
} else {
28-
// For now, only support up to 1 argument
29-
// TODO: Add support for more arguments
30-
const result = Module.ccall(
31-
'eval_ruby_script_returning_json1',
34+
// 3 or more arguments - use json3 (max 3 args supported)
35+
result = Module.ccall(
36+
'eval_ruby_script_returning_json3',
3237
'string',
33-
['string', 'string'],
34-
[text, JSON.stringify(args[0])]
38+
['string', 'string', 'string', 'string'],
39+
[text, JSON.stringify(args[0]), JSON.stringify(args[1]), JSON.stringify(args[2])]
3540
);
36-
return JSON.parse(result);
3741
}
42+
return JSON.parse(result);
3843
}

src/main.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,84 @@ pub extern "C" fn eval_ruby_script_returning_json1(
120120
}
121121
}
122122
}
123+
124+
#[unsafe(no_mangle)]
125+
pub extern "C" fn eval_ruby_script_returning_json2(
126+
text_ptr: *const c_char,
127+
arg1_ptr: *const c_char,
128+
arg2_ptr: *const c_char,
129+
) -> *const c_char {
130+
unsafe {
131+
let text = cstr_to_str(text_ptr);
132+
let arg1 = cstr_to_str(arg1_ptr);
133+
let arg2 = cstr_to_str(arg2_ptr);
134+
match eval_ruby_script_with_setup(text, |vm| {
135+
let arg1_str = RObject::string(arg1.to_string()).to_refcount_assigned();
136+
let arg1_json = mrubyedge_serde_json::mrb_json_class_load(vm, &[arg1_str])
137+
.unwrap_or_else(|_| RObject::nil().to_refcount_assigned());
138+
vm.globals.insert("$arg1".to_string(), arg1_json);
139+
140+
let arg2_str = RObject::string(arg2.to_string()).to_refcount_assigned();
141+
let arg2_json = mrubyedge_serde_json::mrb_json_class_load(vm, &[arg2_str])
142+
.unwrap_or_else(|_| RObject::nil().to_refcount_assigned());
143+
vm.globals.insert("$arg2".to_string(), arg2_json);
144+
}) {
145+
Some(result) => {
146+
let s: String = (&*result).try_into().unwrap_or_default();
147+
match CString::new(s) {
148+
Ok(cstring) => {
149+
let ptr = cstring.as_ptr();
150+
LAST_STRING_RESULT = Some(cstring);
151+
ptr
152+
}
153+
Err(_) => std::ptr::null(),
154+
}
155+
}
156+
None => std::ptr::null(),
157+
}
158+
}
159+
}
160+
161+
#[unsafe(no_mangle)]
162+
pub extern "C" fn eval_ruby_script_returning_json3(
163+
text_ptr: *const c_char,
164+
arg1_ptr: *const c_char,
165+
arg2_ptr: *const c_char,
166+
arg3_ptr: *const c_char,
167+
) -> *const c_char {
168+
unsafe {
169+
let text = cstr_to_str(text_ptr);
170+
let arg1 = cstr_to_str(arg1_ptr);
171+
let arg2 = cstr_to_str(arg2_ptr);
172+
let arg3 = cstr_to_str(arg3_ptr);
173+
match eval_ruby_script_with_setup(text, |vm| {
174+
let arg1_str = RObject::string(arg1.to_string()).to_refcount_assigned();
175+
let arg1_json = mrubyedge_serde_json::mrb_json_class_load(vm, &[arg1_str])
176+
.unwrap_or_else(|_| RObject::nil().to_refcount_assigned());
177+
vm.globals.insert("$arg1".to_string(), arg1_json);
178+
179+
let arg2_str = RObject::string(arg2.to_string()).to_refcount_assigned();
180+
let arg2_json = mrubyedge_serde_json::mrb_json_class_load(vm, &[arg2_str])
181+
.unwrap_or_else(|_| RObject::nil().to_refcount_assigned());
182+
vm.globals.insert("$arg2".to_string(), arg2_json);
183+
184+
let arg3_str = RObject::string(arg3.to_string()).to_refcount_assigned();
185+
let arg3_json = mrubyedge_serde_json::mrb_json_class_load(vm, &[arg3_str])
186+
.unwrap_or_else(|_| RObject::nil().to_refcount_assigned());
187+
vm.globals.insert("$arg3".to_string(), arg3_json);
188+
}) {
189+
Some(result) => {
190+
let s: String = (&*result).try_into().unwrap_or_default();
191+
match CString::new(s) {
192+
Ok(cstring) => {
193+
let ptr = cstring.as_ptr();
194+
LAST_STRING_RESULT = Some(cstring);
195+
ptr
196+
}
197+
Err(_) => std::ptr::null(),
198+
}
199+
}
200+
None => std::ptr::null(),
201+
}
202+
}
203+
}

0 commit comments

Comments
 (0)