瀏覽代碼

Style: Prevent clang-format on JS code

(cherry picked from commit 39114178a0952e9f5efee08e23d73d70f3d8d3db)
Rémi Verschelde 8 年之前
父節點
當前提交
2d479aa0c6

+ 105 - 89
platform/javascript/audio_server_javascript.cpp

@@ -198,14 +198,15 @@ RID AudioServerJavascript::voice_create(){
 	voice->positional=false;
 	voice->active=false;
 
+	/* clang-format off */
 	EM_ASM_( {
-			_as_voices[$0]=null;
-			_as_voice_gain[$0]=_as_audioctx.createGain();
-			_as_voice_pan[$0]=_as_audioctx.createStereoPanner();
-			 _as_voice_gain[$0].connect(_as_voice_pan[$0]);
-			 _as_voice_pan[$0].connect(_as_audioctx.destination);
-
-		},voice_base);
+		_as_voices[$0] = null;
+		_as_voice_gain[$0] = _as_audioctx.createGain();
+		_as_voice_pan[$0] = _as_audioctx.createStereoPanner();
+		_as_voice_gain[$0].connect(_as_voice_pan[$0]);
+		_as_voice_pan[$0].connect(_as_audioctx.destination);
+	}, voice_base);
+	/* clang-format on */
 
 	voice_base++;
 
@@ -227,27 +228,29 @@ void AudioServerJavascript::voice_play(RID p_voice, RID p_sample){
 		//create sample if not created
 		ERR_FAIL_COND(sample->tmp_data.size()==0);
 		sample->index=sample_base;
-		EM_ASM_( {
-			_as_samples[$0]=_as_audioctx.createBuffer($1,$2,$3);
-			},sample_base,sample->stereo?2:1,sample->length,sample->mix_rate);
+		/* clang-format off */
+		EM_ASM_({
+			_as_samples[$0] = _as_audioctx.createBuffer($1, $2, $3);
+		}, sample_base, sample->stereo ? 2 : 1, sample->length, sample->mix_rate);
+		/* clang-format on */
 
 		sample_base++;
 		int chans = sample->stereo?2:1;
 
 
 		for(int i=0;i<chans;i++) {
-
-
+			/* clang-format off */
 			EM_ASM_({
-				       _as_edited_buffer=_as_samples[$0].getChannelData($1);
-			       },sample->index,i);
-
+				_as_edited_buffer = _as_samples[$0].getChannelData($1);
+			}, sample->index, i);
+			/* clang-format on */
 
 			for(int j=0;j<sample->length;j++) {
-
+				/* clang-format off */
 				EM_ASM_({
-					       _as_edited_buffer[$0]=$1;
-				       },j,sample->tmp_data[j*chans+i]);
+					_as_edited_buffer[$0] = $1;
+				}, j, sample->tmp_data[j * chans + i]);
+				/* clang-format on */
 			}
 		}
 
@@ -263,26 +266,27 @@ void AudioServerJavascript::voice_play(RID p_voice, RID p_sample){
 	float freq_diff = Math::log(float(voice->mix_rate)/float(voice->sample_mix_rate))/Math::log(2.0);
 	int detune = int(freq_diff*1200.0);
 
-	EM_ASM_( {
-			if (_as_voices[$0]!==null) {
-				 _as_voices[$0].stop(); //stop and byebye
-			 }
-			 _as_voices[$0]=_as_audioctx.createBufferSource();
-			_as_voices[$0].connect(_as_voice_gain[$0]);
-			_as_voices[$0].buffer=_as_samples[$1];
-			_as_voices[$0].loopStart.value=$1;
-			_as_voices[$0].loopEnd.value=$2;
-			_as_voices[$0].loop.value=$3;
-			_as_voices[$0].detune.value=$6;
-			_as_voice_pan[$0].pan.value=$4;
-			 _as_voice_gain[$0].gain.value=$5;
-			_as_voices[$0].start();
-			_as_voices[$0].onended=function() {
-				 _as_voices[$0].disconnect(_as_voice_gain[$0]);
-				 _as_voices[$0]=null;
-			 }
-
-		},voice->index,sample->index,sample->mix_rate*sample->loop_begin,sample->mix_rate*sample->loop_end,sample->loop_format!=SAMPLE_LOOP_NONE,voice->pan,voice->volume*fx_volume_scale,detune);
+	/* clang-format off */
+	EM_ASM_({
+		if (_as_voices[$0] !== null) {
+			_as_voices[$0].stop(); //stop and byebye
+		}
+		_as_voices[$0] = _as_audioctx.createBufferSource();
+		_as_voices[$0].connect(_as_voice_gain[$0]);
+		_as_voices[$0].buffer = _as_samples[$1];
+		_as_voices[$0].loopStart.value = $1;
+		_as_voices[$0].loopEnd.value = $2;
+		_as_voices[$0].loop.value = $3;
+		_as_voices[$0].detune.value = $6;
+		_as_voice_pan[$0].pan.value = $4;
+		_as_voice_gain[$0].gain.value = $5;
+		_as_voices[$0].start();
+		_as_voices[$0].onended = function() {
+			_as_voices[$0].disconnect(_as_voice_gain[$0]);
+			_as_voices[$0] = null;
+		}
+	}, voice->index, sample->index, sample->mix_rate * sample->loop_begin, sample->mix_rate * sample->loop_end, sample->loop_format != SAMPLE_LOOP_NONE, voice->pan, voice->volume * fx_volume_scale, detune);
+	/* clang-format on */
 
 	voice->active=true;
 }
@@ -295,11 +299,11 @@ void AudioServerJavascript::voice_set_volume(RID p_voice, float p_volume){
 	voice->volume=p_volume;
 
 	if (voice->active) {
-		EM_ASM_( {
-
-			_as_voice_gain[$0].gain.value=$1;
-
-			},voice->index,voice->volume*fx_volume_scale);
+		/* clang-format off */
+		EM_ASM_({
+			_as_voice_gain[$0].gain.value = $1;
+		}, voice->index, voice->volume * fx_volume_scale);
+		/* clang-format on */
 	}
 
 }
@@ -313,11 +317,11 @@ void AudioServerJavascript::voice_set_pan(RID p_voice, float p_pan, float p_dept
 	voice->pan_height=height;
 
 	if (voice->active) {
-		EM_ASM_( {
-
-			_as_voice_pan[$0].pan.value=$1;
-
-			},voice->index,voice->pan);
+		/* clang-format off */
+		EM_ASM_({
+			_as_voice_pan[$0].pan.value = $1;
+		}, voice->index, voice->pan);
+		/* clang-format on */
 	}
 }
 void AudioServerJavascript::voice_set_filter(RID p_voice, FilterType p_type, float p_cutoff, float p_resonance, float p_gain){
@@ -340,11 +344,11 @@ void AudioServerJavascript::voice_set_mix_rate(RID p_voice, int p_mix_rate){
 
 		float freq_diff = Math::log(float(voice->mix_rate)/float(voice->sample_mix_rate))/Math::log(2.0);
 		int detune = int(freq_diff*1200.0);
-		EM_ASM_( {
-
-			_as_voices[$0].detune.value=$1;
-
-			},voice->index,detune);
+		/* clang-format off */
+		EM_ASM_({
+			_as_voices[$0].detune.value = $1;
+		}, voice->index, detune);
+		/* clang-format on */
 	}
 }
 void AudioServerJavascript::voice_set_positional(RID p_voice, bool p_positional){
@@ -419,14 +423,15 @@ void AudioServerJavascript::voice_stop(RID p_voice){
 	ERR_FAIL_COND(!voice);
 
 	if (voice->active) {
-
-		EM_ASM_( {
-				 if (_as_voices[$0]!==null) {
-					_as_voices[$0].stop();
-					 _as_voices[$0].disconnect(_as_voice_gain[$0]);
-					 _as_voices[$0]=null;
-				 }
-			},voice->index);
+		/* clang-format off */
+		EM_ASM_({
+			if (_as_voices[$0] !== null) {
+				_as_voices[$0].stop();
+				_as_voices[$0].disconnect(_as_voice_gain[$0]);
+				_as_voices[$0] = null;
+			}
+		}, voice->index);
+		/* clang-format on */
 
 		voice->active=false;
 	}
@@ -524,22 +529,25 @@ void AudioServerJavascript::free(RID p_id){
 		ERR_FAIL_COND(!voice);
 
 		if (voice->active) {
-			EM_ASM_( {
-				 if (_as_voices[$0]!==null) {
+			/* clang-format off */
+			EM_ASM_({
+				 if (_as_voices[$0] !== null) {
 					_as_voices[$0].stop();
-					 _as_voices[$0].disconnect(_as_voice_gain[$0]);
+					_as_voices[$0].disconnect(_as_voice_gain[$0]);
 				 }
-			},voice->index);
+			}, voice->index);
+			/* clang-format on */
 		}
 
-		EM_ASM_( {
+		/* clang-format off */
+		EM_ASM_({
 			delete _as_voices[$0];
 			_as_voice_gain[$0].disconnect(_as_voice_pan[$0]);
 			delete _as_voice_gain[$0];
 			_as_voice_pan[$0].disconnect(_as_audioctx.destination);
 			delete _as_voice_pan[$0];
-
-		 },voice->index);
+		}, voice->index);
+		/* clang-format on */
 
 		voice_owner.free(p_id);
 		memdelete(voice);
@@ -549,10 +557,11 @@ void AudioServerJavascript::free(RID p_id){
 		Sample *sample = sample_owner.get(p_id);
 		ERR_FAIL_COND(!sample);
 
-		EM_ASM_( {
+		/* clang-format off */
+		EM_ASM_({
 			delete _as_samples[$0];
-
-		 },sample->index);
+		}, sample->index);
+		/* clang-format on */
 
 		sample_owner.free(p_id);
 		memdelete(sample);
@@ -594,21 +603,20 @@ void AudioServerJavascript::mix_to_js(int p_frames) {
 		int tomix=MIN(todo,INTERNAL_BUFFER_SIZE);
 		driver_process_chunk(tomix);
 
-
+		/* clang-format off */
 		EM_ASM_({
-
-			var data = HEAPF32.subarray($0/4, $0/4 + $2*2);
+			var data = HEAPF32.subarray($0 / 4, $0 / 4 + $2 * 2);
 
 			for (var channel = 0; channel < _as_output_buffer.numberOfChannels; channel++) {
 				var outputData = _as_output_buffer.getChannelData(channel);
 				// Loop through samples
 				for (var sample = 0; sample < $2; sample++) {
 					// make output equal to the same as the input
-					outputData[sample+$1] = data[sample*2+channel];
+					outputData[sample + $1] = data[sample * 2 + channel];
 				}
 			}
-
-			},internal_buffer,offset,tomix);
+		}, internal_buffer, offset, tomix);
+		/* clang-format on */
 
 		todo-=tomix;
 		offset+=tomix;
@@ -617,9 +625,13 @@ void AudioServerJavascript::mix_to_js(int p_frames) {
 
 void AudioServerJavascript::init(){
 
-	//EM_ASM(
-//		console.log('server is '+audio_server);
-//	);
+	/*
+	// clang-format off
+	EM_ASM(
+		console.log('server is ' + audio_server);
+	);
+	// clang-format on
+	*/
 
 
 	//int latency = GLOBAL_DEF("javascript/audio_latency",16384);
@@ -632,19 +644,19 @@ void AudioServerJavascript::init(){
 
 	int buffer_latency=16384;
 
+	/* clang-format off */
 	EM_ASM_( {
-
 		_as_script_node = _as_audioctx.createScriptProcessor($0, 0, 2);
 		_as_script_node.connect(_as_audioctx.destination);
 		console.log(_as_script_node.bufferSize);
 
-
 		_as_script_node.onaudioprocess = function(audioProcessingEvent) {
 		// The output buffer contains the samples that will be modified and played
 			_as_output_buffer = audioProcessingEvent.outputBuffer;
 			audio_server_mix_function(_as_output_buffer.getChannelData(0).length);
 		}
-	},buffer_latency);
+	}, buffer_latency);
+	/* clang-format on */
 
 
 }
@@ -811,20 +823,24 @@ AudioServerJavascript::AudioServerJavascript() {
 	singleton=this;
 	sample_base=1;
 	voice_base=1;
+	/* clang-format off */
 	EM_ASM(
-		_as_samples={};
-		_as_voices={};
-		_as_voice_pan={};
-		_as_voice_gain={};
+		_as_samples = {};
+		_as_voices = {};
+		_as_voice_pan = {};
+		_as_voice_gain = {};
 
 		_as_audioctx = new (window.AudioContext || window.webkitAudioContext)();
 
 		audio_server_mix_function = Module.cwrap('audio_server_mix_function', 'void', ['number']);
 	);
+	/* clang-format on */
 
+	/* clang-format off */
 	webaudio_mix_rate = EM_ASM_INT_V(
-				return _as_audioctx.sampleRate;
-					);
+		return _as_audioctx.sampleRate;
+	);
+	/* clang-format on */
 	print_line("WEBAUDIO MIX RATE: "+itos(webaudio_mix_rate));
 	event_voice_scale=1.0;
 	fx_volume_scale=1.0;

+ 172 - 0
platform/javascript/javascript_eval.cpp

@@ -0,0 +1,172 @@
+/*************************************************************************/
+/*  javascript_eval.cpp                                                  */
+/*************************************************************************/
+/*                       This file is part of:                           */
+/*                           GODOT ENGINE                                */
+/*                    http://www.godotengine.org                         */
+/*************************************************************************/
+/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur.                 */
+/*                                                                       */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the       */
+/* "Software"), to deal in the Software without restriction, including   */
+/* without limitation the rights to use, copy, modify, merge, publish,   */
+/* distribute, sublicense, and/or sell copies of the Software, and to    */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions:                                             */
+/*                                                                       */
+/* The above copyright notice and this permission notice shall be        */
+/* included in all copies or substantial portions of the Software.       */
+/*                                                                       */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
+/*************************************************************************/
+#ifdef JAVASCRIPT_EVAL_ENABLED
+
+#include "javascript_eval.h"
+#include "emscripten.h"
+
+JavaScript *JavaScript::singleton=NULL;
+
+JavaScript *JavaScript::get_singleton() {
+
+	return singleton;
+}
+
+Variant JavaScript::eval(const String& p_code, bool p_use_global_exec_context) {
+
+	union { int i; double d; char* s; } js_data[4];
+	/* clang-format off */
+	Variant::Type return_type = static_cast<Variant::Type>(EM_ASM_INT({
+
+		var eval_ret;
+		try {
+			if ($3) { // p_use_global_exec_context
+				// indirect eval call grants global execution context
+				var global_eval = eval;
+				eval_ret = global_eval(UTF8ToString($2));
+			} else {
+				eval_ret = eval(UTF8ToString($2));
+			}
+		} catch (e) {
+			Module.printErr(e);
+			eval_ret = null;
+		}
+
+		switch (typeof eval_ret) {
+
+			case 'boolean':
+				// bitwise op yields 32-bit int
+				setValue($0, eval_ret|0, 'i32');
+				return 1; // BOOL
+
+			case 'number':
+				if ((eval_ret|0)===eval_ret) {
+					setValue($0, eval_ret|0, 'i32');
+					return 2; // INT
+				}
+				setValue($0, eval_ret, 'double');
+				return 3; // REAL
+
+			case 'string':
+				var array_len = lengthBytesUTF8(eval_ret)+1;
+				var array_ptr = _malloc(array_len);
+				try {
+					if (array_ptr===0) {
+						throw new Error('String allocation failed (probably out of memory)');
+					}
+					setValue($0, array_ptr|0 , '*');
+					stringToUTF8(eval_ret, array_ptr, array_len);
+					return 4; // STRING
+				} catch (e) {
+					if (array_ptr!==0) {
+						_free(array_ptr)
+					}
+					Module.printErr(e);
+					// fall through
+				}
+				break;
+
+			case 'object':
+				if (eval_ret === null) {
+					break;
+				}
+
+				else if (typeof eval_ret.x==='number' && typeof eval_ret.y==='number') {
+					setValue($0, eval_ret.x, 'double');
+					setValue($0+$1, eval_ret.y, 'double');
+					if (typeof eval_ret.z==='number') {
+						setValue($0+$1*2, eval_ret.z, 'double');
+						return 7; // VECTOR3
+					}
+					else if (typeof eval_ret.width==='number' && typeof eval_ret.height==='number') {
+						setValue($0+$1*2, eval_ret.width, 'double');
+						setValue($0+$1*3, eval_ret.height, 'double');
+						return 6; // RECT2
+					}
+					return 5; // VECTOR2
+				}
+
+				else if (typeof eval_ret.r==='number' && typeof eval_ret.g==='number' && typeof eval_ret.b==='number') {
+					// assume 8-bit rgb components since we're on the web
+					setValue($0, eval_ret.r, 'double');
+					setValue($0+$1, eval_ret.g, 'double');
+					setValue($0+$1*2, eval_ret.b, 'double');
+					setValue($0+$1*3, typeof eval_ret.a==='number' ? eval_ret.a : 1, 'double');
+					return 14; // COLOR
+				}
+				break;
+		}
+		return 0; // NIL
+
+	}, js_data, sizeof *js_data, p_code.utf8().get_data(), p_use_global_exec_context));
+	/* clang-format on */
+
+	switch(return_type) {
+		case Variant::BOOL:
+			return !!js_data->i;
+		case Variant::INT:
+			return js_data->i;
+		case Variant::REAL:
+			return js_data->d;
+		case Variant::STRING:
+			{
+				String str = String::utf8(js_data->s);
+				/* clang-format off */
+				EM_ASM_({ _free($0); }, js_data->s);
+				/* clang-format on */
+				return str;
+			}
+		case Variant::VECTOR2:
+			return Vector2(js_data[0].d, js_data[1].d);
+		case Variant::VECTOR3:
+			return Vector3(js_data[0].d, js_data[1].d, js_data[2].d);
+		case Variant::RECT2:
+			return Rect2(js_data[0].d, js_data[1].d, js_data[2].d, js_data[3].d);
+		case Variant::COLOR:
+			return Color(js_data[0].d/255., js_data[1].d/255., js_data[2].d/255., js_data[3].d);
+	}
+	return Variant();
+}
+
+void JavaScript::_bind_methods() {
+
+	ObjectTypeDB::bind_method(_MD("eval", "code", "use_global_execution_context"), &JavaScript::eval, false);
+}
+
+JavaScript::JavaScript() {
+
+	ERR_FAIL_COND(singleton != NULL);
+	singleton = this;
+}
+
+JavaScript::~JavaScript() {
+
+}
+
+#endif // JAVASCRIPT_EVAL_ENABLED

+ 2 - 0
platform/javascript/javascript_main.cpp

@@ -181,6 +181,7 @@ int main(int argc, char *argv[]) {
 
 
 	 //mount persistent filesystem
+	/* clang-format off */
 	 EM_ASM(
 		 FS.mkdir('/userfs');
 		 FS.mount(IDBFS, {}, '/userfs');
@@ -198,6 +199,7 @@ int main(int argc, char *argv[]) {
 		 });
 
 	  );
+	/* clang-format on */
 
 	glutMainLoop();
 

+ 14 - 2
platform/javascript/os_javascript.cpp

@@ -215,6 +215,7 @@ void OS_JavaScript::initialize(const VideoMode& p_desired,int p_video_driver,int
 
 	// find locale, emscripten only sets "C"
 	char locale_ptr[16];
+	/* clang-format off */
 	EM_ASM_({
 		var locale = "";
 		if (Module.locale) {
@@ -230,6 +231,7 @@ void OS_JavaScript::initialize(const VideoMode& p_desired,int p_video_driver,int
 		locale = locale.split('.')[0];
 		stringToUTF8(locale, $0, 16);
 	}, locale_ptr);
+	/* clang-format on */
 	setenv("LANG", locale_ptr, true);
 
 	print_line("Init Audio");
@@ -327,9 +329,11 @@ void OS_JavaScript::finalize() {
 
 void OS_JavaScript::alert(const String& p_alert,const String& p_title) {
 
+	/* clang-format off */
 	EM_ASM_({
 		window.alert(UTF8ToString($0));
 	}, p_alert.utf8().get_data());
+	/* clang-format on */
 }
 
 
@@ -361,9 +365,11 @@ int OS_JavaScript::get_mouse_button_state() const {
 
 void OS_JavaScript::set_window_title(const String& p_title) {
 
+	/* clang-format off */
 	EM_ASM_({
 		document.title = UTF8ToString($0);
 	}, p_title.utf8().get_data());
+	/* clang-format on */
 }
 
 //interesting byt not yet
@@ -419,8 +425,10 @@ void OS_JavaScript::set_window_maximized(bool p_enabled) {
 			set_window_fullscreen(false);
 		}
 		else {
+			/* clang-format off */
 			video_mode.width = EM_ASM_INT_V(return window.innerWidth);
 			video_mode.height = EM_ASM_INT_V(return window.innerHeight);
+			/* clang-format on */
 			emscripten_set_canvas_size(video_mode.width, video_mode.height);
 		}
 	}
@@ -439,7 +447,9 @@ void OS_JavaScript::set_window_fullscreen(bool p_enable) {
 	// _browser_resize_callback or _fullscreen_change_callback
 	EMSCRIPTEN_RESULT result;
 	if (p_enable) {
+		/* clang-format off */
 		EM_ASM(Module.requestFullscreen(false, false););
+		/* clang-format on */
 	}
 	else {
 		result = emscripten_exit_fullscreen();
@@ -501,8 +511,7 @@ bool OS_JavaScript::main_loop_iterate() {
 
 		if (time_to_save_sync<0) {
 			//time to sync, for real
-			// run 'success'
-			print_line("DOING SYNCH!");
+			/* clang-format off */
 			EM_ASM(
 			  FS.syncfs(function (err) {
 			    assert(!err);
@@ -510,6 +519,7 @@ bool OS_JavaScript::main_loop_iterate() {
 			    //ccall('success', 'v');
 			  });
 			);
+			/* clang-format on */
 		}
 
 
@@ -786,9 +796,11 @@ void OS_JavaScript::reload_gfx() {
 }
 
 Error OS_JavaScript::shell_open(String p_uri) {
+	/* clang-format off */
 	EM_ASM_({
 		window.open(UTF8ToString($0), '_blank');
 	}, p_uri.utf8().get_data());
+	/* clang-format on */
 	return OK;
 }