<commit_before>-module(beamjs).
-include_lib("erlv8/include/erlv8.hrl").
-export([start/0,stop/0,main/0,
		 bundles/0, set_bundles/1, modules/1, modules/2,
		 load_default_mods/1]).

start() ->
	application:start(beamjs).

stop() ->
	application:stop(beamjs).

bundles() ->
		case application:get_env(beamjs, bundles) of
			{ok, Bundles0} ->
				Bundles0;
			_ ->
				[default,runtime]
		end.

modules(Type,Bundle) ->
	case application:get_env(beamjs,module_bundles) of
		{ok, MB} ->
			proplists:get_value(Type, proplists:get_value(Bundle, MB, []), []);
		_ ->
			[]
	end.

modules(Type) ->
	lists:flatten(lists:map(fun (B) -> modules(Type, B) end, bundles())).

set_bundles(Bundles) ->
	application:set_env(beamjs, bundles, lists:usort([default,runtime|Bundles])).


%%%

load_default_mods(VM) ->
	lists:foreach(fun({Name,Mod}) ->
						  erlv8_vm:register(VM,Name,Mod)
				  end, modules(default)).

args(bundles) ->
	case init:get_argument(bundles) of
		{ok, [Bundles]} ->
			set_bundles(lists:map(fun list_to_atom/1,Bundles));
		_ ->
			false
	end;

args(norepl) ->
	case init:get_argument(norepl) of
		{ok, _} ->
			true;
		_ ->
			false
	end;
args(toolbar) ->
	case init:get_argument(toolbar) of
		{ok, _} ->
			toolbar:start();
		_ ->
			false
	end;

args({mod, Arg, Type}) ->
	case init:get_argument(Arg) of
		{ok, [[Alias, Mod]]} ->
			{MB, RuntimeBundle0} = 
				case application:get_env(beamjs,module_bundles) of
					{ok, MB0} ->
						{MB0, proplists:get_value(runtime,MB0,[])};
					_ ->
						application:set_env(beamjs,module_bundles, []),
						{[], []}
				end,
			TRuntimeModules0 = proplists:get_value(Type, RuntimeBundle0, []),
			TRuntimeModules = [{Alias, list_to_atom(Mod)}|TRuntimeModules0],
			RuntimeBundle = [{Type, TRuntimeModules}|proplists:delete(Type, RuntimeBundle0)],
			application:set_env(beamjs,module_bundles,[{runtime, RuntimeBundle}|proplists:delete(runtime, MB)]);
		_ ->
			false
	end;

args(mod) ->
	args({mod, mod, available}).


args(VM,jseval) ->
	case init:get_argument(jseval) of
		{ok, [[JS]]} ->
			erlv8_vm:run(VM, erlv8_context:get(VM), JS, {"(command line)",0,0});
		_ ->
			false
	end;

args(VM,default_mod) ->
	args({mod, default_mod, default}),
	case init:get_argument(default_mod) of
		{ok, [[Alias, Mod]]} ->
			erlv8_vm:register(VM,Alias,list_to_atom(Mod));
		_ ->
			false
	end;

args(VM, path) ->
	case init:get_argument(jspath) of
		{ok, [Paths]} ->
			lists:foreach(fun (Path) ->
								  Global = erlv8_vm:global(VM),
								  Require = Global:get_value("require"),
								  RPaths = Require:get_value("paths"),
								  RPaths:unshift(Path)
						  end, Paths);
		_ -> 
			false
	end;
			
args(VM,load) ->
	case init:get_argument(load) of
		{ok, [Files]} ->
			lists:foreach(fun (File) ->
								  Global = erlv8_vm:global(VM),
								  Require = Global:get_value("require"),
								  Global:set_value("module",?V8Obj([])),
								  Module = Global:get_value("module"),
								  Module:set_value("id",File,[dontdelete,readonly]),
								  Require:set_value("main",Module),
								  case Require:call([File]) of
									  {throw, {error, #erlv8_object{}=E}} ->
										  io:format("~s~n",[beamjs_js_formatter:format_exception(VM,E)]);
									  _ ->
										  ignore
								  end
						  end, Files);
		_ ->
			false
	end.

	
-define(REPL_START,"require('repl').start()").

main() ->
	case os:getenv("ERLV8_SO_PATH") of
		false ->
			os:putenv("ERLV8_SO_PATH","./deps/erlv8/priv")
	end,
	erlv8:start(),
	start(),
	{ok, VM} = erlv8_vm:start(),
	args(bundles),
	load_default_mods(VM),
	NoRepl = args(norepl),
	args(toolbar),
	args(mod),
	args(VM,default_mod),
	args(VM,jseval),
	args(VM,path),
	args(VM,load),
	case NoRepl of
		true ->
			ok;
		false ->
			Global = erlv8_vm:global(VM),
			Global:set_value("module",?V8Obj([{"exports", ?V8Obj([])}])),
			Module = Global:get_value("module"),
			Module:set_value("id","repl",[readonly,dontdelete]),
			Require = Global:get_value("require"),
			Require:set_value("main",Module,[readonly,dontdelete]),
			erlv8_vm:run(VM, erlv8_context:get(VM), ?REPL_START, {"main",0,0})
	end,
	erlang:halt().

<commit_msg>Expose proper contents of beamjs_mod_repl:exports/1 in REPL<commit_after>-module(beamjs).
-include_lib("erlv8/include/erlv8.hrl").
-export([start/0,stop/0,main/0,
		 bundles/0, set_bundles/1, modules/1, modules/2,
		 load_default_mods/1]).

start() ->
	application:start(beamjs).

stop() ->
	application:stop(beamjs).

bundles() ->
		case application:get_env(beamjs, bundles) of
			{ok, Bundles0} ->
				Bundles0;
			_ ->
				[default,runtime]
		end.

modules(Type,Bundle) ->
	case application:get_env(beamjs,module_bundles) of
		{ok, MB} ->
			proplists:get_value(Type, proplists:get_value(Bundle, MB, []), []);
		_ ->
			[]
	end.

modules(Type) ->
	lists:flatten(lists:map(fun (B) -> modules(Type, B) end, bundles())).

set_bundles(Bundles) ->
	application:set_env(beamjs, bundles, lists:usort([default,runtime|Bundles])).


%%%

load_default_mods(VM) ->
	lists:foreach(fun({Name,Mod}) ->
						  erlv8_vm:register(VM,Name,Mod)
				  end, modules(default)).

args(bundles) ->
	case init:get_argument(bundles) of
		{ok, [Bundles]} ->
			set_bundles(lists:map(fun list_to_atom/1,Bundles));
		_ ->
			false
	end;

args(norepl) ->
	case init:get_argument(norepl) of
		{ok, _} ->
			true;
		_ ->
			false
	end;
args(toolbar) ->
	case init:get_argument(toolbar) of
		{ok, _} ->
			toolbar:start();
		_ ->
			false
	end;

args({mod, Arg, Type}) ->
	case init:get_argument(Arg) of
		{ok, [[Alias, Mod]]} ->
			{MB, RuntimeBundle0} = 
				case application:get_env(beamjs,module_bundles) of
					{ok, MB0} ->
						{MB0, proplists:get_value(runtime,MB0,[])};
					_ ->
						application:set_env(beamjs,module_bundles, []),
						{[], []}
				end,
			TRuntimeModules0 = proplists:get_value(Type, RuntimeBundle0, []),
			TRuntimeModules = [{Alias, list_to_atom(Mod)}|TRuntimeModules0],
			RuntimeBundle = [{Type, TRuntimeModules}|proplists:delete(Type, RuntimeBundle0)],
			application:set_env(beamjs,module_bundles,[{runtime, RuntimeBundle}|proplists:delete(runtime, MB)]);
		_ ->
			false
	end;

args(mod) ->
	args({mod, mod, available}).


args(VM,jseval) ->
	case init:get_argument(jseval) of
		{ok, [[JS]]} ->
			erlv8_vm:run(VM, erlv8_context:get(VM), JS, {"(command line)",0,0});
		_ ->
			false
	end;

args(VM,default_mod) ->
	args({mod, default_mod, default}),
	case init:get_argument(default_mod) of
		{ok, [[Alias, Mod]]} ->
			erlv8_vm:register(VM,Alias,list_to_atom(Mod));
		_ ->
			false
	end;

args(VM, path) ->
	case init:get_argument(jspath) of
		{ok, [Paths]} ->
			lists:foreach(fun (Path) ->
								  Global = erlv8_vm:global(VM),
								  Require = Global:get_value("require"),
								  RPaths = Require:get_value("paths"),
								  RPaths:unshift(Path)
						  end, Paths);
		_ -> 
			false
	end;
			
args(VM,load) ->
	case init:get_argument(load) of
		{ok, [Files]} ->
			lists:foreach(fun (File) ->
								  Global = erlv8_vm:global(VM),
								  Require = Global:get_value("require"),
								  Global:set_value("module",?V8Obj([])),
								  Module = Global:get_value("module"),
								  Module:set_value("id",File,[dontdelete,readonly]),
								  Require:set_value("main",Module),
								  case Require:call([File]) of
									  {throw, {error, #erlv8_object{}=E}} ->
										  io:format("~s~n",[beamjs_js_formatter:format_exception(VM,E)]);
									  _ ->
										  ignore
								  end
						  end, Files);
		_ ->
			false
	end.

	
-define(REPL_START,"require('repl').start()").

main() ->
	case os:getenv("ERLV8_SO_PATH") of
		false ->
			os:putenv("ERLV8_SO_PATH","./deps/erlv8/priv")
	end,
	erlv8:start(),
	start(),
	{ok, VM} = erlv8_vm:start(),
	args(bundles),
	load_default_mods(VM),
	NoRepl = args(norepl),
	args(toolbar),
	args(mod),
	args(VM,default_mod),
	args(VM,jseval),
	args(VM,path),
	args(VM,load),
	case NoRepl of
		true ->
			ok;
		false ->
			Global = erlv8_vm:global(VM),
			Global:set_value("module",?V8Obj([{"exports", beamjs_mod_repl:exports(VM)}])),
			Module = Global:get_value("module"),
			Module:set_value("id","repl",[readonly,dontdelete]),
			Require = Global:get_value("require"),
			Require:set_value("main",Module,[readonly,dontdelete]),
			erlv8_vm:run(VM, erlv8_context:get(VM), ?REPL_START, {"main",0,0})
	end,
	erlang:halt().


{'fraction_non_alphanumeric': 0.18336733469338773, 'fraction_numerical': 0.01032041281651266, 'mean_word_length': 3.8062283737024223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1014802', 'n_tokens_mistral': 3875, 'n_tokens_neox': 3233, 'n_words': 474}