123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159(* Js_of_ocaml library
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2014 Hugo Heuzard
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)openJs_of_ocamlopenJs_of_ocaml_compileropenJs_of_ocaml_compiler.Stdlibletsplit_primitivesp=letlen=String.lengthpinletrecsplitbegcur=ifcur>=lenthen[]elseifp.[cur]='\000'thenString.subp~pos:beg~len:(cur-beg)::split(cur+1)(cur+1)elsesplitbeg(cur+1)inArray.of_list(split00)letsetup=lazy(Topdirs.dir_directory"/static/cmis";Hashtbl.addToploop.directive_table"enable"(Toploop.Directive_stringConfig.Flag.enable);Hashtbl.addToploop.directive_table"disable"(Toploop.Directive_stringConfig.Flag.disable);Hashtbl.addToploop.directive_table"debug_on"(Toploop.Directive_stringDebug.enable);Hashtbl.addToploop.directive_table"debug_off"(Toploop.Directive_stringDebug.disable);Hashtbl.addToploop.directive_table"tailcall"(Toploop.Directive_string(Config.Param.set"tc"));letinitial_primitive_count=Array.length(split_primitives(Symtable.data_primitive_names()))in(* this needs to stay synchronized with toplevel.js *)letcompile(s:stringarray)=lets=String.concat~sep:""(Array.to_lists)inletprims=split_primitives(Symtable.data_primitive_names())inletunbound_primitivep=tryignore(Js.Unsafe.eval_stringp);falsewith_->trueinletstubs=ref[]inArray.iteriprims~f:(funip->ifi>=initial_primitive_count&&unbound_primitivepthenstubs:=Format.sprintf"function %s(){caml_failwith(\"%s not implemented\")}"pp::!stubs);letoutput_program=Driver.from_stringprimssinletb=Buffer.create100inoutput_program(Pretty_print.to_bufferb);Format.(pp_print_flushstd_formatter());Format.(pp_print_flusherr_formatter());flushstdout;flushstderr;letres=Buffer.contentsbinletres=String.concat~sep:""!stubs^resinletres:unit->_=Js.Unsafe.global##toplevelEvalresinresinJs.Unsafe.global##.toplevelCompile:=compile(*XXX HACK!*);(Js.Unsafe.global##.toplevelEval:=funx->letf:<..>Js.t-><..>Js.t=Js.Unsafe.eval_stringxinfun()->letres=fJs.Unsafe.globalinFormat.(pp_print_flushstd_formatter());Format.(pp_print_flusherr_formatter());flushstdout;flushstderr;res);Js.Unsafe.global##.toplevelReloc:=Js.Unsafe.callback(funname->letname=Js.to_stringnameinJs_of_ocaml_toplevel_compat.relocname);())letrefill_lexbufspppfbufferlen=if!p=String.lengthsthen0elseletlen',nl=tryString.index_froms!p'\n'-!p+1,falsewith_->String.lengths-!p,trueinletlen''=minlenlen'inString.blit~src:s~src_pos:!p~dst:buffer~dst_pos:0~len:len'';(matchppfwith|Someppf->Format.fprintfppf"%s"(Bytes.sub_stringbuffer~pos:0~len:len'');ifnlthenFormat.pp_print_newlineppf();Format.pp_print_flushppf()|None->());p:=!p+len'';len''letuseffpcontent=letname="/dev/fake_stdin"inifSys.file_existsnamethenSys.removename;Sys_js.create_file~name~content;Toploop.use_silentlyffpnameletexecuteprintval?pp_code?highlight_locationpp_answers=letlb=Lexing.from_function(refill_lexbufs(ref0)pp_code)in(trywhiletruedotryletphr=!Toploop.parse_toplevel_phraselbinletphr=JsooTopPpx.preprocess_phrasephrinignore(Toploop.execute_phraseprintvalpp_answerphr:bool)with|End_of_file->raiseEnd_of_file|x->(matchhighlight_locationwith|None->()|Somef->(matchJsooTopError.locxwith|None->()|Someloc->floc));Errors.report_errorFormat.err_formatterxdonewithEnd_of_file->());flush_all()letinitialize()=Sys.interactive:=false;Lazy.forcesetup;Toploop.initialize_toplevel_env();Toploop.input_name:="//toplevel//";Sys.interactive:=true