Skip to content

Commit

Permalink
Add support for print()
Browse files Browse the repository at this point in the history
  • Loading branch information
joehoyle committed May 8, 2022
1 parent 68f2a5c commit b4f2f8c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ The extension includes builds of libv8, via the [v8 crate](https://docs.rs/v8/la
- [x] Memory / time limits
- [x] Snapshop creating and loading
- [x] Default global functions `var_dump`, `sleep`, `exit`
- [ ] Default global function `print`
- [x] Default global function `print`
- [x] CommonJS / `require` support
- [x] `setModuleLoader`
- [ ] `setModuleNormaliser`
- [ ] Subclassing V8Js
- [x] Custom exceptions for `V8JsScriptException`, `V8JsMemoryLimitException` and `V8JsTimeLimitException`
- [ ] Support for `V8JsScriptException::getJsLineNumber` etc.
- [ ] Support for `FLAG_PROPAGATE_PHP_EXCEPTIONS`, `V8Js::FLAG_FORCE_ARRAY`
- [ ] Throw correct exception subclasses
- [ ] PHP INI settings `v8js.flags`
- [x] `V8Js::V8_VERSION` constant

Expand Down
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ext_php_rs::flags::DataType;
use ext_php_rs::types::{ZendHashTable, ZendObject, Zval};
use ext_php_rs::zend::{ClassEntry, ModuleEntry};
use ext_php_rs::{exception::PhpException, zend::ce};
use ext_php_rs::{info_table_end, info_table_row, info_table_start, prelude::*};
use ext_php_rs::{info_table_end, info_table_row, info_table_start, prelude::*, php_print};

use std::collections::HashMap;

Expand Down Expand Up @@ -166,7 +166,7 @@ impl V8Js {
}
runtime.add_global(global_name.as_str(), object);
runtime.add_global_function("var_dump", php_callback_var_dump);
runtime.add_global_function("print", php_callback_var_dump);
runtime.add_global_function("print", php_callback_print);
runtime.add_global_function("exit", php_callback_exit);
runtime.add_global_function("sleep", php_callback_sleep);
runtime.add_global_function("require", php_callback_require);
Expand Down Expand Up @@ -375,6 +375,14 @@ pub fn php_callback_var_dump(
rv.set(result);
}

pub fn php_callback_print(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
mut _rv: v8::ReturnValue,
) {
php_print!("{}", args.get(0).to_rust_string_lossy(scope) );
}

pub fn php_callback_exit(
scope: &mut v8::HandleScope,
_args: v8::FunctionCallbackArguments,
Expand Down
31 changes: 18 additions & 13 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,19 +256,24 @@ impl JSRuntime {
None => {
let exception = try_catch.exception().unwrap();
let exception_string = exception
.to_string(try_catch)
.unwrap()
.to_rust_string_lossy(try_catch);
let message = try_catch.message().unwrap();
Err(Error::ScriptExecutionError(ScriptExecutionErrorData {
file_name: message.get_script_resource_name(try_catch).unwrap().to_rust_string_lossy(try_catch),
line_number: u64::try_from(message.get_line_number(try_catch).unwrap()).unwrap(),
start_column: u64::try_from(message.get_start_column()).unwrap(),
end_column: u64::try_from(message.get_end_column()).unwrap(),
trace: "".into(), // todo,
message: exception_string,
source_line: message.get_source_line(try_catch).unwrap().to_rust_string_lossy(try_catch),
}))
.to_string(try_catch);

match exception_string {
Some(exception_string) => {
let exception_string = exception_string.to_rust_string_lossy(try_catch);
let message = try_catch.message().unwrap();
Err(Error::ScriptExecutionError(ScriptExecutionErrorData {
file_name: message.get_script_resource_name(try_catch).unwrap().to_rust_string_lossy(try_catch),
line_number: u64::try_from(message.get_line_number(try_catch).unwrap()).unwrap(),
start_column: u64::try_from(message.get_start_column()).unwrap(),
end_column: u64::try_from(message.get_end_column()).unwrap(),
trace: "".into(), // todo,
message: exception_string,
source_line: message.get_source_line(try_catch).unwrap().to_rust_string_lossy(try_catch),
}))
}
None => Ok(None)
}
}
};
result
Expand Down
10 changes: 5 additions & 5 deletions tests/global_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

// Todo: print needs implementing.

// $v8js = new V8Js;
// ob_start();
// $v8js->executeString( 'print("hello")' );
// $result = ob_get_clean();
// assert( $result === "hello");
$v8js = new V8Js;
ob_start();
$v8js->executeString( 'print("hello")' );
$result = ob_get_clean();
assert( $result === "hello");

$v8js = new V8Js;
ob_start();
Expand Down

0 comments on commit b4f2f8c

Please sign in to comment.