Add support for finding Javascript source libraries with Emscripten.
diff --git a/cross/wasm.txt b/cross/wasm.txt
index f2d0cd7..4289c00 100644
--- a/cross/wasm.txt
+++ b/cross/wasm.txt
@@ -1,12 +1,12 @@
 [binaries]
-c = '/home/jpakkane/emsdk/fastcomp/emscripten/emcc'
-cpp = '/home/jpakkane/emsdk/fastcomp/emscripten/em++'
-ar = '/home/jpakkane/emsdk/fastcomp/emscripten/emar'
+c = '/home/jpakkane/src/emsdk/upstream/emscripten/emcc'
+cpp = '/home/jpakkane/src/emsdk/upstream/emscripten/em++'
+ar = '/home/jpakkane/src/emsdk/upstream/emscripten/emar'
 
 [built-in options]
-c_args = ['-s', 'WASM=1', '-s', 'EXPORT_ALL=1']
+c_args = []
 c_link_args = ['-s','EXPORT_ALL=1']
-cpp_args = ['-s', 'WASM=1', '-s', 'EXPORT_ALL=1']
+cpp_args = []
 cpp_link_args = ['-s', 'EXPORT_ALL=1']
 
 [host_machine]
diff --git a/docs/markdown/snippets/wasmlibs.md b/docs/markdown/snippets/wasmlibs.md
new file mode 100644
index 0000000..15434d1
--- /dev/null
+++ b/docs/markdown/snippets/wasmlibs.md
@@ -0,0 +1,13 @@
+## Add support for `find_library` in Emscripten
+
+The `find_library` method can be used to find your own JavaScript
+libraries. The limitation is that they must have the file extension
+`.js`. Other library lookups will look up "native" libraries from the
+system like currently. A typical usage would look like this:
+
+```meson
+glue_lib = cc.find_library('gluefuncs.js',
+                           dirs: meson.current_source_dir())
+executable('prog', 'prog.c',
+           dependencies: glue_lib)
+```
diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py
index fc01ac0..5c1d9d4 100644
--- a/mesonbuild/compilers/compilers.py
+++ b/mesonbuild/compilers/compilers.py
@@ -47,7 +47,8 @@
 
 header_suffixes = ('h', 'hh', 'hpp', 'hxx', 'H', 'ipp', 'moc', 'vapi', 'di')  # type: T.Tuple[str, ...]
 obj_suffixes = ('o', 'obj', 'res')  # type: T.Tuple[str, ...]
-lib_suffixes = ('a', 'lib', 'dll', 'dll.a', 'dylib', 'so')  # type: T.Tuple[str, ...]
+# To the emscripten compiler, .js files are libraries
+lib_suffixes = ('a', 'lib', 'dll', 'dll.a', 'dylib', 'so', 'js')  # type: T.Tuple[str, ...]
 # Mapping of language to suffixes of files that should always be in that language
 # This means we can't include .h headers here since they could be C, C++, ObjC, etc.
 lang_suffixes = {
diff --git a/mesonbuild/compilers/mixins/emscripten.py b/mesonbuild/compilers/mixins/emscripten.py
index 226cc15..c770639 100644
--- a/mesonbuild/compilers/mixins/emscripten.py
+++ b/mesonbuild/compilers/mixins/emscripten.py
@@ -18,7 +18,9 @@
 import typing as T
 
 from ... import coredata
+from ... import mesonlib
 from ...mesonlib import OptionKey
+from ...mesonlib import LibType
 
 if T.TYPE_CHECKING:
     from ...environment import Environment
@@ -67,3 +69,33 @@
         })
 
         return opts
+
+    def wrap_js_includes(self, args: T.List[str]):
+        final_args = []
+        for i in args:
+            if i.endswith('.js') and not i.startswith('-'):
+                final_args += ['--js-library', i]
+            else:
+                final_args += [i]
+        return final_args
+
+    def native_args_to_unix(cls, args: T.List[str]) -> T.List[str]:
+        return cls.wrap_js_includes(super().native_args_to_unix(args))
+
+    def get_dependency_link_args(self, dep: 'Dependency') -> T.List[str]:
+        return self.wrap_js_includes(super().get_dependency_link_args(dep))
+
+    def find_library(self, libname: str, env: 'Environment', extra_dirs: T.List[str],
+                     libtype: LibType = LibType.PREFER_SHARED) -> T.Optional[T.List[str]]:
+        if not libname.endswith('.js'):
+            return super().find_library(libname, env, extra_dirs, libtype)
+        if os.path.isabs(libname):
+            if os.path.exists(libname):
+                return libname
+        if len(extra_dirs) == 0:
+            raise mesonlib.EnvironmentException('Looking up Emscripten JS libraries requires either an absolute path or specifying extra_dirs.')
+        for d in extra_dirs:
+            abs_path = os.path.join(d, libname)
+            if os.path.exists(abs_path):
+                return [abs_path]
+        return None
diff --git a/test cases/wasm/3 jslib/meson.build b/test cases/wasm/3 jslib/meson.build
new file mode 100644
index 0000000..3cc1740
--- /dev/null
+++ b/test cases/wasm/3 jslib/meson.build
@@ -0,0 +1,8 @@
+project('jslib', 'c')
+
+cc = meson.get_compiler('c')
+
+sf_dep = cc.find_library('somefuncs.js', dirs: meson.current_source_dir())
+
+executable('libcallc', 'prog.c',
+    dependencies: sf_dep)
\ No newline at end of file
diff --git a/test cases/wasm/3 jslib/prog.c b/test cases/wasm/3 jslib/prog.c
new file mode 100644
index 0000000..fa2a9c5
--- /dev/null
+++ b/test cases/wasm/3 jslib/prog.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+#include <emscripten.h>
+
+// This code should have a reference to the Javascript sampleFunction defined in the
+// library file. I don't know how to write it so it does not do that.
+//
+// Patches welcome.
+
+int main() {
+  printf("Hello World\n");
+  // sampleFunction(); ????
+  return 0;
+}
diff --git a/test cases/wasm/3 jslib/somefuncs.js b/test cases/wasm/3 jslib/somefuncs.js
new file mode 100644
index 0000000..b120859
--- /dev/null
+++ b/test cases/wasm/3 jslib/somefuncs.js
@@ -0,0 +1,3 @@
+function sampleFunction() {
+    alert("Something happened!");
+}