fix memory leaks, add bufsize option for httpserver/static.spl

This commit is contained in:
Daniella / Tove 2024-10-13 23:10:16 +02:00
parent 456d39399e
commit c1475cc153
Signed by: TudbuT
GPG key ID: B3CF345217F202D3
4 changed files with 18 additions and 16 deletions

View file

@ -3,6 +3,8 @@
"_static_ext_server" net:http:server:register "_static_ext_server" net:http:server:register
"_static_ext_Request" net:http:server:register "_static_ext_Request" net:http:server:register
"bufsize" net:http:server:register
1024 net:http:server:=bufsize
construct net:http:server:_static_ext_server { construct net:http:server:_static_ext_server {
cached-files cached-files
@ -16,14 +18,14 @@ construct net:http:server:_static_ext_Request {
; ;
serve-file { this | with filepath path type this ; serve-file { this | with filepath path type this ;
this:path path eq if { this:path path eq if {
filepath StreamTypes:file:create<0>:read-to-end<1024> this:write-ok:write-content-type<type>:write-body:finish; filepath StreamTypes:file:create<0>:read-to-end<net:http:server:bufsize> this:write-ok:write-content-type<type>:write-body:finish;
} }
this this
} }
serve-file-cached { this | with filepath path type this ; serve-file-cached { this | with filepath path type this ;
this:path path eq if { this:path path eq if {
filepath this:server:get-cached-files:get dup not if { filepath this:server:get-cached-files:get dup not if {
pop filepath StreamTypes:file:create<0>:read-to-end<1024> dup this:server:cached-files:set;<filepath> pop filepath StreamTypes:file:create<0>:read-to-end<net:http:server:bufsize> dup this:server:cached-files:set;<filepath>
} }
this:write-ok:write-content-type<type>:write-body:finish; this:write-ok:write-content-type<type>:write-body:finish;
} }

View file

@ -92,7 +92,7 @@ construct _mega-ext {
while { !!- i this lt } { !!- i callable call i 1 + =i } while { !!- i this lt } { !!- i callable call i 1 + =i }
} }
fforeach { | with callable this ; fforeach { | with callable this ;
0 while { !!- dup2 this lt } { !!- callable call 1 + } 0 while { !!- dup2 this lt } { !!- callable callp 1 + }
} }
_str_radix { str | with radix this ; _str_radix { str | with radix this ;
this radix mega-to-str-radix this radix mega-to-str-radix

View file

@ -474,6 +474,9 @@ impl Stack {
} }
pub fn call(&mut self, func: &AFunc) -> OError { pub fn call(&mut self, func: &AFunc) -> OError {
if func.origin.is_dummy() {
return self.fast_call(func);
}
let f = if let Some(ref cname) = func.fname { let f = if let Some(ref cname) = func.fname {
Frame::new_in( Frame::new_in(
func.origin.clone(), func.origin.clone(),
@ -516,14 +519,13 @@ impl Stack {
frame = self.frames.first().unwrap().clone(); frame = self.frames.first().unwrap().clone();
} }
let tmpname = name.clone(); let tmpname = name.clone();
let tmpframe = frame.clone();
frame.functions.lock().insert( frame.functions.lock().insert(
name.clone(), name.clone(),
Arc::new(Func { Arc::new(Func {
ret_count: 1, ret_count: 1,
origin: frame.clone(), origin: Arc::new(Frame::dummy()),
to_call: FuncImpl::NativeDyn(Arc::new(Box::new(move |stack| { to_call: FuncImpl::NativeDyn(Arc::new(Box::new(move |stack| {
stack.push(tmpframe.get_var(tmpname.clone(), stack)?); stack.push(stack.get_frame().get_var(tmpname.clone(), stack)?);
Ok(()) Ok(())
}))), }))),
run_as_base: false, run_as_base: false,
@ -532,15 +534,14 @@ impl Stack {
}), }),
); );
let tmpname = name.clone(); let tmpname = name.clone();
let tmpframe = frame.clone();
frame.functions.lock().insert( frame.functions.lock().insert(
"=".to_owned() + &name, "=".to_owned() + &name,
Arc::new(Func { Arc::new(Func {
ret_count: 0, ret_count: 0,
origin: frame.clone(), origin: Arc::new(Frame::dummy()),
to_call: FuncImpl::NativeDyn(Arc::new(Box::new(move |stack| { to_call: FuncImpl::NativeDyn(Arc::new(Box::new(move |stack| {
let v = stack.pop(); let v = stack.pop();
tmpframe.set_var(tmpname.clone(), v, stack) stack.get_frame().set_var(tmpname.clone(), v, stack)
}))), }))),
run_as_base: false, run_as_base: false,
fname: Some("RUNTIME".to_owned()), fname: Some("RUNTIME".to_owned()),

View file

@ -145,7 +145,7 @@ pub fn array_len(stack: &mut Stack) -> OError {
pub fn array_get(stack: &mut Stack) -> OError { pub fn array_get(stack: &mut Stack) -> OError {
let binding = stack.pop(); let binding = stack.pop();
let Value::Mega(i) = stack.pop().lock_ro().native.clone() else { let Value::Mega(i) = stack.pop().lock_ro().native else {
return stack.err(ErrorKind::InvalidCall("array-get".to_owned())); return stack.err(ErrorKind::InvalidCall("array-get".to_owned()));
}; };
let o = match binding.lock_ro().native { let o = match binding.lock_ro().native {
@ -158,15 +158,14 @@ pub fn array_get(stack: &mut Stack) -> OError {
} }
pub fn array_set(stack: &mut Stack) -> OError { pub fn array_set(stack: &mut Stack) -> OError {
let binding = stack.pop(); let binding = &stack.pop();
let Value::Mega(i) = stack.pop().lock_ro().native.clone() else { let binding = &mut binding.lock().native;
return stack.err(ErrorKind::InvalidCall("array-set".to_owned())); require_on_stack!(i, Mega, stack, "array-set");
};
let o = stack.pop(); let o = stack.pop();
if let Value::Array(ref mut a) = binding.lock().native { if let Value::Array(ref mut a) = binding {
stack.push(a.get(i as usize).ok_or_else(array!(stack, i))?.clone()); stack.push(a.get(i as usize).ok_or_else(array!(stack, i))?.clone());
*a.get_mut(i as usize).ok_or_else(array!(stack, i))? = o; *a.get_mut(i as usize).ok_or_else(array!(stack, i))? = o;
} else if let Value::ByteArray(ref mut a) = binding.lock().native { } else if let Value::ByteArray(ref mut a) = binding {
let Value::Int(o) = o.lock_ro().native else { let Value::Int(o) = o.lock_ro().native else {
return stack.err(ErrorKind::InvalidCall("array-set".to_owned())); return stack.err(ErrorKind::InvalidCall("array-set".to_owned()));
}; };