Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] HELP! In-place modification of caml globals...
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jonathan Roewen <jonathan.roewen@g...>
Subject: [Caml-list] HELP! In-place modification of caml globals...
Hi,

I need some help, badly ;-) This is the one thing getting in my way at
present, and I don't know how to fix it.

I have two types:

type in_channel = {
	mutable in_read : unit -> char;
	mutable in_input : string -> int -> int -> int;
	mutable in_seek : int -> unit;
	mutable in_tell : unit -> int;
	mutable in_close : unit -> unit;
	mutable in_length : unit -> int;
}

type 'a out_channel = {
	mutable out_write : char -> unit;
	mutable out_output : string -> int -> int -> unit;
	mutable out_seek : int -> unit;
	mutable out_tell : unit -> int;
	mutable out_close : unit -> 'a;
	mutable out_flush : unit -> unit;
	mutable out_length : unit -> int;
}

and three globals:

let stdin =
	let read = (fun _ -> raise Input_closed)
	and input = (fun _ _ _ -> raise Input_closed)
	and seek = (fun _ -> raise Input_closed)
	and tell = (fun _ -> raise Input_closed)
	in create_in read input seek tell default_close default_length

let stdout =
	let write = (fun _ -> raise Output_closed)
	and output = (fun _ _ _ -> raise Output_closed)
	and seek = (fun _ -> raise Output_closed)
	and tell = (fun _ -> raise Output_closed)
	in create_out write output seek tell default_close default_flush default_length

let stderr =
	let write = (fun _ -> raise Output_closed)
	and output = (fun _ _ _ -> raise Output_closed)
	and seek = (fun _ -> raise Output_closed)
	and tell = (fun _ -> raise Output_closed)
	in create_out write output seek tell default_close default_flush default_length

By default, they're all closed. What I want to do is swap them with
other instances at kernel startup.

So I defined two external functions:

external swap_input : in_channel -> in_channel -> unit = "caml_ml_swap_input";;
external swap_output : unit out_channel -> unit out_channel -> unit =
"caml_ml_swap_output";;

Which are both defined as:

value caml_ml_swap_input(value to, value from) {
	/* record has 6 fields */
	Store_field(to, 0, Field(from, 0));
	Store_field(to, 1, Field(from, 1));
	Store_field(to, 2, Field(from, 2));
	Store_field(to, 3, Field(from, 3));
	Store_field(to, 4, Field(from, 4));
	Store_field(to, 5, Field(from, 5));
	return Val_unit;
}

value caml_ml_swap_output(value to, value from) {
	/* record has 7 fields */
	Store_field(to, 0, Field(from, 0));
	Store_field(to, 1, Field(from, 1));
	Store_field(to, 2, Field(from, 2));
	Store_field(to, 3, Field(from, 3));
	Store_field(to, 4, Field(from, 4));
	Store_field(to, 5, Field(from, 5));
	Store_field(to, 6, Field(from, 6));
	return Val_unit;
}

Then initialise with:

(* init the standard IO descriptors *)
swap_input stdin kernel_stdin;;
swap_output stdout kernel_stdout;;
swap_output stderr kernel_stderr;;

However, as soon as I try to print, I get a fatal error that output
stream is closed =/

What's the correct way to do this?

Jonathan