Skip to content

Commit

Permalink
feat(napi): export a pure parse function for benchmark purposes (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Jul 19, 2023
1 parent e1dec30 commit 3f84a7f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
13 changes: 12 additions & 1 deletion crates/oxc_napi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,22 @@ export interface ParserOptions {
sourceFilename?: string
}
export interface ParseResult {
program: any
program: string
errors: Array<string>
}
/**
* Parse without returning anything.
* For benchmark purposes such as measuring the napi communication overhead.
*
* # Panics
*
* * File extension is invalid
* * Serde JSON serialization
*/
export function parseWithoutReturn(sourceText: string, options?: ParserOptions | undefined | null): void
/**
* # Panics
*
* * File extension is invalid
* * Serde JSON serialization
*/
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_napi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { parseSync, parseAsync } = nativeBinding
const { parseWithoutReturn, parseSync, parseAsync } = nativeBinding

module.exports.parseWithoutReturn = parseWithoutReturn
module.exports.parseSync = parseSync
module.exports.parseAsync = parseAsync
49 changes: 37 additions & 12 deletions crates/oxc_napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use miette::NamedSource;
use napi_derive::napi;
use oxc_allocator::Allocator;
pub use oxc_ast::ast::Program;
use oxc_parser::Parser;
use oxc_parser::{Parser, ParserReturn};
use oxc_span::SourceType;

/// Babel Parser Options
Expand All @@ -22,18 +22,15 @@ pub struct ParserOptions {

#[napi(object)]
pub struct ParseResult {
pub program: serde_json::Value,
pub program: String,
pub errors: Vec<String>,
}

/// # Panics
/// * File extension is invalid
/// * Serde JSON serialization
#[allow(clippy::needless_pass_by_value)]
#[napi]
pub fn parse_sync(source_text: String, options: Option<ParserOptions>) -> ParseResult {
let options = options.unwrap_or_default();

fn parse<'a>(
allocator: &'a Allocator,
source_text: &'a str,
options: &ParserOptions,
) -> ParserReturn<'a> {
let source_type = options
.source_filename
.as_ref()
Expand All @@ -44,10 +41,36 @@ pub fn parse_sync(source_text: String, options: Option<ParserOptions>) -> ParseR
Some("module") => source_type.with_module(true),
_ => source_type,
};
Parser::new(allocator, source_text, source_type).parse()
}

/// Parse without returning anything.
/// This is for benchmark purposes such as measuring napi communication overhead.
///
/// # Panics
///
/// * File extension is invalid
/// * Serde JSON serialization
#[allow(clippy::needless_pass_by_value)]
#[napi]
pub fn parse_without_return(source_text: String, options: Option<ParserOptions>) {
let options = options.unwrap_or_default();
let allocator = Allocator::default();
let ret = Parser::new(&allocator, &source_text, source_type).parse();
let program = serde_json::to_value(&ret.program).unwrap();
parse(&allocator, &source_text, &options);
}

/// # Panics
///
/// * File extension is invalid
/// * Serde JSON serialization
#[allow(clippy::needless_pass_by_value)]
#[napi]
pub fn parse_sync(source_text: String, options: Option<ParserOptions>) -> ParseResult {
let options = options.unwrap_or_default();

let allocator = Allocator::default();
let ret = parse(&allocator, &source_text, &options);
let program = serde_json::to_string(&ret.program).unwrap();

let errors = if ret.errors.is_empty() {
vec![]
Expand All @@ -60,10 +83,12 @@ pub fn parse_sync(source_text: String, options: Option<ParserOptions>) -> ParseR
.map(|error| format!("{error:?}"))
.collect()
};

ParseResult { program, errors }
}

/// # Panics
///
/// * Tokio crashes
#[allow(clippy::needless_pass_by_value)]
#[napi]
Expand Down

0 comments on commit 3f84a7f

Please sign in to comment.