Skip to content

Commit add60f0

Browse files
authored
Merge pull request #12 from kirencore/claude/issue-11-20250802-2101
Fix Rust compilation errors in express.rs
2 parents 60022c4 + f0c616e commit add60f0

File tree

1 file changed

+55
-35
lines changed

1 file changed

+55
-35
lines changed

src/api/express.rs

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -624,15 +624,19 @@ fn register_route(scope: &mut v8::HandleScope, args: v8::FunctionCallbackArgumen
624624
// Create mock request and response objects to test function behavior
625625
let mock_req = create_mock_request(scope, method, &path);
626626
let mock_res = create_mock_response(scope);
627-
627+
628628
// Execute the function with mock objects
629629
let args = [mock_req.into(), mock_res.into()];
630-
if let Some(_result) = func.call(scope, v8::undefined(scope).into(), &args) {
630+
let undefined = v8::undefined(scope);
631+
if let Some(_result) = func.call(scope, undefined.into(), &args) {
631632
// Extract the response pattern from the mock response
632633
extract_response_pattern(scope, mock_res, method, &path)
633634
} else {
634635
// Fallback if execution fails
635-
format!("function(req, res) {{ res.send('Default response for {} {}'); }}", method, path)
636+
format!(
637+
"function(req, res) {{ res.send('Default response for {} {}'); }}",
638+
method, path
639+
)
636640
}
637641
} else {
638642
// Fallback for functions that can't be converted
@@ -669,76 +673,76 @@ fn create_mock_request<'a>(
669673
path: &str,
670674
) -> v8::Local<'a, v8::Object> {
671675
let req_obj = v8::Object::new(scope);
672-
676+
673677
// Set method
674678
let method_key = v8::String::new(scope, "method").unwrap();
675679
let method_val = v8::String::new(scope, method).unwrap();
676680
req_obj.set(scope, method_key.into(), method_val.into());
677-
681+
678682
// Set path
679683
let path_key = v8::String::new(scope, "path").unwrap();
680684
let path_val = v8::String::new(scope, path).unwrap();
681685
req_obj.set(scope, path_key.into(), path_val.into());
682-
686+
683687
// Set url
684688
let url_key = v8::String::new(scope, "url").unwrap();
685689
req_obj.set(scope, url_key.into(), path_val.into());
686-
690+
687691
// Set empty params and query
688692
let params_key = v8::String::new(scope, "params").unwrap();
689693
let params_obj = v8::Object::new(scope);
690694
req_obj.set(scope, params_key.into(), params_obj.into());
691-
695+
692696
let query_key = v8::String::new(scope, "query").unwrap();
693697
let query_obj = v8::Object::new(scope);
694698
req_obj.set(scope, query_key.into(), query_obj.into());
695-
699+
696700
// Set empty headers
697701
let headers_key = v8::String::new(scope, "headers").unwrap();
698702
let headers_obj = v8::Object::new(scope);
699703
req_obj.set(scope, headers_key.into(), headers_obj.into());
700-
704+
701705
req_obj
702706
}
703707

704708
fn create_mock_response<'a>(scope: &mut v8::HandleScope<'a>) -> v8::Local<'a, v8::Object> {
705709
let res_obj = v8::Object::new(scope);
706-
710+
707711
// Internal state
708712
let status_key = v8::String::new(scope, "_statusCode").unwrap();
709713
let status_val = v8::Number::new(scope, 200.0);
710714
res_obj.set(scope, status_key.into(), status_val.into());
711-
715+
712716
let headers_key = v8::String::new(scope, "_headers").unwrap();
713717
let headers_obj = v8::Object::new(scope);
714718
res_obj.set(scope, headers_key.into(), headers_obj.into());
715-
719+
716720
let body_key = v8::String::new(scope, "_body").unwrap();
717721
let body_val = v8::String::new(scope, "").unwrap();
718722
res_obj.set(scope, body_key.into(), body_val.into());
719-
723+
720724
let finished_key = v8::String::new(scope, "_finished").unwrap();
721725
let finished_val = v8::Boolean::new(scope, false);
722726
res_obj.set(scope, finished_key.into(), finished_val.into());
723-
727+
724728
// Response method - status
725729
let status_method_key = v8::String::new(scope, "status").unwrap();
726730
let status_method_template = v8::FunctionTemplate::new(scope, mock_res_status);
727731
let status_method = status_method_template.get_function(scope).unwrap();
728732
res_obj.set(scope, status_method_key.into(), status_method.into());
729-
733+
730734
// Response method - send
731735
let send_key = v8::String::new(scope, "send").unwrap();
732736
let send_template = v8::FunctionTemplate::new(scope, mock_res_send);
733737
let send_method = send_template.get_function(scope).unwrap();
734738
res_obj.set(scope, send_key.into(), send_method.into());
735-
739+
736740
// Response method - json
737741
let json_key = v8::String::new(scope, "json").unwrap();
738742
let json_template = v8::FunctionTemplate::new(scope, mock_res_json);
739743
let json_method = json_template.get_function(scope).unwrap();
740744
res_obj.set(scope, json_key.into(), json_method.into());
741-
745+
742746
res_obj
743747
}
744748

@@ -753,7 +757,7 @@ fn mock_res_status(
753757
let status_key = v8::String::new(scope, "_statusCode").unwrap();
754758
this.set(scope, status_key.into(), status_code);
755759
}
756-
rv.set(args.this());
760+
rv.set(args.this().into());
757761
}
758762

759763
fn mock_res_send(
@@ -764,7 +768,7 @@ fn mock_res_send(
764768
if args.length() > 0 {
765769
let this = args.this();
766770
let data = args.get(0);
767-
771+
768772
// Set content-type to text/html if not set
769773
let headers_key = v8::String::new(scope, "_headers").unwrap();
770774
if let Some(headers_val) = this.get(scope, headers_key.into()) {
@@ -776,18 +780,18 @@ fn mock_res_send(
776780
}
777781
}
778782
}
779-
783+
780784
// Set body
781785
let body_key = v8::String::new(scope, "_body").unwrap();
782786
let data_str = data.to_string(scope).unwrap();
783787
this.set(scope, body_key.into(), data_str.into());
784-
788+
785789
// Mark as finished
786790
let finished_key = v8::String::new(scope, "_finished").unwrap();
787791
let finished_val = v8::Boolean::new(scope, true);
788792
this.set(scope, finished_key.into(), finished_val.into());
789793
}
790-
rv.set(args.this());
794+
rv.set(args.this().into());
791795
}
792796

793797
fn mock_res_json(
@@ -798,7 +802,7 @@ fn mock_res_json(
798802
if args.length() > 0 {
799803
let this = args.this();
800804
let data = args.get(0);
801-
805+
802806
// Set content-type to application/json
803807
let headers_key = v8::String::new(scope, "_headers").unwrap();
804808
if let Some(headers_val) = this.get(scope, headers_key.into()) {
@@ -808,18 +812,25 @@ fn mock_res_json(
808812
headers_obj.set(scope, ct_key.into(), ct_val.into());
809813
}
810814
}
811-
815+
812816
// Convert data to JSON string and set as body
813817
let body_key = v8::String::new(scope, "_body").unwrap();
814-
let json_str = v8::JSON::stringify(scope, data).unwrap();
818+
let json_key = v8::String::new(scope, "JSON").unwrap();
819+
let global = scope.get_current_context().global(scope);
820+
let json_obj = global.get(scope, json_key.into()).unwrap();
821+
let stringify_key = v8::String::new(scope, "stringify").unwrap();
822+
let json_obj = v8::Local::<v8::Object>::try_from(json_obj).unwrap();
823+
let stringify_fn = json_obj.get(scope, stringify_key.into()).unwrap();
824+
let stringify_fn = v8::Local::<v8::Function>::try_from(stringify_fn).unwrap();
825+
let json_str = stringify_fn.call(scope, json_obj.into(), &[data]).unwrap();
815826
this.set(scope, body_key.into(), json_str.into());
816-
827+
817828
// Mark as finished
818829
let finished_key = v8::String::new(scope, "_finished").unwrap();
819830
let finished_val = v8::Boolean::new(scope, true);
820831
this.set(scope, finished_key.into(), finished_val.into());
821832
}
822-
rv.set(args.this());
833+
rv.set(args.this().into());
823834
}
824835

825836
fn extract_response_pattern(
@@ -833,7 +844,7 @@ fn extract_response_pattern(
833844
if let Some(body_val) = mock_res.get(scope, body_key.into()) {
834845
let body_str = body_val.to_string(scope).unwrap();
835846
let body = body_str.to_rust_string_lossy(scope);
836-
847+
837848
// Get content-type to determine response method
838849
let headers_key = v8::String::new(scope, "_headers").unwrap();
839850
if let Some(headers_val) = mock_res.get(scope, headers_key.into()) {
@@ -842,26 +853,35 @@ fn extract_response_pattern(
842853
if let Some(ct_val) = headers_obj.get(scope, ct_key.into()) {
843854
let ct_str = ct_val.to_string(scope).unwrap();
844855
let content_type = ct_str.to_rust_string_lossy(scope);
845-
856+
846857
if content_type.contains("application/json") {
847858
// User called res.json()
848859
return format!("function(req, res) {{ res.json({}); }}", body);
849860
} else {
850861
// User called res.send() or similar
851-
return format!("function(req, res) {{ res.send('{}'); }}", body.replace("'", "\\'"));
862+
return format!(
863+
"function(req, res) {{ res.send('{}'); }}",
864+
body.replace("'", "\\'")
865+
);
852866
}
853867
}
854868
}
855869
}
856-
870+
857871
// Default to send if we have a body but no content-type
858872
if !body.is_empty() {
859-
return format!("function(req, res) {{ res.send('{}'); }}", body.replace("'", "\\'"));
873+
return format!(
874+
"function(req, res) {{ res.send('{}'); }}",
875+
body.replace("'", "\\'")
876+
);
860877
}
861878
}
862-
879+
863880
// Fallback if we couldn't extract anything meaningful
864-
format!("function(req, res) {{ res.send('Response from {} {}'); }}", method, path)
881+
format!(
882+
"function(req, res) {{ res.send('Response from {} {}'); }}",
883+
method, path
884+
)
865885
}
866886

867887
fn parse_route_pattern(path: &str) -> (String, Vec<String>) {

0 commit comments

Comments
 (0)