Skip to content

Commit b4f2f8c

Browse files
committed
Add support for print()
1 parent 68f2a5c commit b4f2f8c

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

Diff for: README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@ The extension includes builds of libv8, via the [v8 crate](https://docs.rs/v8/la
2828
- [x] Memory / time limits
2929
- [x] Snapshop creating and loading
3030
- [x] Default global functions `var_dump`, `sleep`, `exit`
31-
- [ ] Default global function `print`
31+
- [x] Default global function `print`
3232
- [x] CommonJS / `require` support
3333
- [x] `setModuleLoader`
3434
- [ ] `setModuleNormaliser`
3535
- [ ] Subclassing V8Js
3636
- [x] Custom exceptions for `V8JsScriptException`, `V8JsMemoryLimitException` and `V8JsTimeLimitException`
3737
- [ ] Support for `V8JsScriptException::getJsLineNumber` etc.
3838
- [ ] Support for `FLAG_PROPAGATE_PHP_EXCEPTIONS`, `V8Js::FLAG_FORCE_ARRAY`
39-
- [ ] Throw correct exception subclasses
4039
- [ ] PHP INI settings `v8js.flags`
4140
- [x] `V8Js::V8_VERSION` constant
4241

Diff for: src/lib.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use ext_php_rs::flags::DataType;
55
use ext_php_rs::types::{ZendHashTable, ZendObject, Zval};
66
use ext_php_rs::zend::{ClassEntry, ModuleEntry};
77
use ext_php_rs::{exception::PhpException, zend::ce};
8-
use ext_php_rs::{info_table_end, info_table_row, info_table_start, prelude::*};
8+
use ext_php_rs::{info_table_end, info_table_row, info_table_start, prelude::*, php_print};
99

1010
use std::collections::HashMap;
1111

@@ -166,7 +166,7 @@ impl V8Js {
166166
}
167167
runtime.add_global(global_name.as_str(), object);
168168
runtime.add_global_function("var_dump", php_callback_var_dump);
169-
runtime.add_global_function("print", php_callback_var_dump);
169+
runtime.add_global_function("print", php_callback_print);
170170
runtime.add_global_function("exit", php_callback_exit);
171171
runtime.add_global_function("sleep", php_callback_sleep);
172172
runtime.add_global_function("require", php_callback_require);
@@ -375,6 +375,14 @@ pub fn php_callback_var_dump(
375375
rv.set(result);
376376
}
377377

378+
pub fn php_callback_print(
379+
scope: &mut v8::HandleScope,
380+
args: v8::FunctionCallbackArguments,
381+
mut _rv: v8::ReturnValue,
382+
) {
383+
php_print!("{}", args.get(0).to_rust_string_lossy(scope) );
384+
}
385+
378386
pub fn php_callback_exit(
379387
scope: &mut v8::HandleScope,
380388
_args: v8::FunctionCallbackArguments,

Diff for: src/runtime.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -256,19 +256,24 @@ impl JSRuntime {
256256
None => {
257257
let exception = try_catch.exception().unwrap();
258258
let exception_string = exception
259-
.to_string(try_catch)
260-
.unwrap()
261-
.to_rust_string_lossy(try_catch);
262-
let message = try_catch.message().unwrap();
263-
Err(Error::ScriptExecutionError(ScriptExecutionErrorData {
264-
file_name: message.get_script_resource_name(try_catch).unwrap().to_rust_string_lossy(try_catch),
265-
line_number: u64::try_from(message.get_line_number(try_catch).unwrap()).unwrap(),
266-
start_column: u64::try_from(message.get_start_column()).unwrap(),
267-
end_column: u64::try_from(message.get_end_column()).unwrap(),
268-
trace: "".into(), // todo,
269-
message: exception_string,
270-
source_line: message.get_source_line(try_catch).unwrap().to_rust_string_lossy(try_catch),
271-
}))
259+
.to_string(try_catch);
260+
261+
match exception_string {
262+
Some(exception_string) => {
263+
let exception_string = exception_string.to_rust_string_lossy(try_catch);
264+
let message = try_catch.message().unwrap();
265+
Err(Error::ScriptExecutionError(ScriptExecutionErrorData {
266+
file_name: message.get_script_resource_name(try_catch).unwrap().to_rust_string_lossy(try_catch),
267+
line_number: u64::try_from(message.get_line_number(try_catch).unwrap()).unwrap(),
268+
start_column: u64::try_from(message.get_start_column()).unwrap(),
269+
end_column: u64::try_from(message.get_end_column()).unwrap(),
270+
trace: "".into(), // todo,
271+
message: exception_string,
272+
source_line: message.get_source_line(try_catch).unwrap().to_rust_string_lossy(try_catch),
273+
}))
274+
}
275+
None => Ok(None)
276+
}
272277
}
273278
};
274279
result

Diff for: tests/global_functions.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
// Todo: print needs implementing.
44

5-
// $v8js = new V8Js;
6-
// ob_start();
7-
// $v8js->executeString( 'print("hello")' );
8-
// $result = ob_get_clean();
9-
// assert( $result === "hello");
5+
$v8js = new V8Js;
6+
ob_start();
7+
$v8js->executeString( 'print("hello")' );
8+
$result = ob_get_clean();
9+
assert( $result === "hello");
1010

1111
$v8js = new V8Js;
1212
ob_start();

0 commit comments

Comments
 (0)