<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">
	<channel>
		<title>Author at Infinum</title>
		<atom:link href="https://infinum.com/blog/author/matija/feed/" rel="self" type="application/rss+xml" />
		<link></link>
		<description>Building digital products</description>
		<lastBuildDate>Mon, 27 Apr 2026 14:58:27 +0000</lastBuildDate>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>

					<item>
				<image>
					<url>7977https://infinum.com/uploads/2014/04/running-javascript-in-an-ios-application-with-javascriptcore-0.webp</url>
				</image>
				<title>Running JavaScript in an iOS Application with JavaScriptCore</title>
				<link>https://infinum.com/blog/running-javascript-in-an-ios-application-with-javascriptcore/</link>
				<pubDate>Fri, 11 Apr 2014 11:28:00 +0000</pubDate>
				<dc:creator>Matija Undefined</dc:creator>
				<guid isPermaLink="false">https://infinum.com/the-capsized-eight/running-javascript-in-an-ios-application-with-javascriptcore/</guid>
				<description>
					<![CDATA[<p>Although JavaScriptCore has been officially available to iOS and Mac developers for some time, its features are still documented rather poorly. </p>
<p>The post <a href="https://infinum.com/blog/running-javascript-in-an-ios-application-with-javascriptcore/">Running JavaScript in an iOS Application with JavaScriptCore</a> appeared first on <a href="https://infinum.com">Infinum</a>.</p>
]]>
				</description>
				<content:encoded>
					<![CDATA[<div
	class="wrapper"
	data-id="es-204"
	 data-animation-target='inner-items'>
		
			<div class="wrapper__inner">
			<div class="block-blog-content js-block-blog-content">
	
<div class="block-blog-content-sidebar" data-id="es-92">
	</div>

<div class="block-blog-content-main">
	
<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-95"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-93">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-94'
	>
	Although the JavaScriptCore framework has been officially available to iOS and Mac developers for quite some time now, its features are still documented rather poorly. Also, Apple provides no guidelines, save for a great WWDC session named “Integrating JavaScript into Native Apps”.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-98"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-96">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-97'
	>
	However, while waiting for them to update the documentation, there’s no reason not to take advantage of the framework’s capabilities – it is, as will be demonstrated in this blog post, fairly easy to use.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-101"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-99">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-100'
	>
	Where to begin?</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-104"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-102">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-103'
	>
	There are three object types we will use to provide a quick example of what you can do with JavaScriptCore. These are:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-107"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="lists" data-id="es-105">
	<ul	class='typography typography--size-16-text-roman js-typography lists__typography'
	data-id='es-106'
	>
	<li><code>JSVirtualMachine</code></li><li><code>JSContext</code></li><li><code>JSValue</code></li></ul></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-110"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-media">
	<div	class="media block-media__media media__border--none media__align--center-center"
	data-id="es-108"
	 data-media-type='image'>

	<figure class="image block-media__image-figure image--size-stretch" data-id="es-109">
	<picture class="image__picture block-media__image-picture">
												<img
					src="https://infinum.com/uploads/2014/04/running-javascript-in-an-ios-application-with-javascriptcore-1.webp"
					class="image__img block-media__image-img"
					alt=""
										height="555"
															width="600"
										loading="lazy"
					 />
					</picture>

			<figcaption class="image__figcaption block-media__image-figcaption">
			Keep in mind that the number of virtual machines is not limited by the number of ARM cores		</figcaption>
	</figure></div></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-113"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-111">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-112'
	>
	<code>JSVirtualMachine</code> represents a JavaScript virtual machine, which manages memory and garbage collection. An arbitrary number of these objects can be instantiated in order to execute JavaScript code in parallel – each of them uses a separate thread.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-116"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-114">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-115'
	>
	<code>JSContext</code> sounds a bit trickier to decipher, but rest assured it makes sense. For JavaScript code to be executed, it needs to have a context, a space where it can create new variables and access existing ones. So, a <code>JSContext</code> is a code evaluation context, and you can have multiple contexts in a single virtual machine. You can also exchange information between contexts in the same virtual machine, but not between virtual machines.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-119"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-117">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-118'
	>
	<code>JSValue</code> is a class representing any type of data in a JavaScript context. You can have as many <code>JSValue</code>s as you want in a context and you can pass them between different contexts as long as those contexts belong to the same virtual machine.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-122"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-120">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-121'
	>
	How to use it?</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-125"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-123">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-124'
	>
	To execute JavaScript code, we need to have a virtual machine and context. And here’s how we can create a JavaScript context:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-127"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-c github-light" data-language="c" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token">JSContext </span><span class="token" style="color: #d73a49;">*</span><span class="token">context </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token">[</span><span class="token">[</span><span class="token">JSContext alloc</span><span class="token">]</span><span class="token"> initWithVirtualMachine:</span><span class="token">[</span><span class="token">[</span><span class="token">JSVirtualMachine alloc</span><span class="token">]</span><span class="token"> init</span><span class="token">]</span><span class="token">]</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-130"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-128">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-129'
	>
	One thing worth mentioning is that the context is empty when we create it. It contains no variables or functions. There’s more than one way to create variables in a context or assign values to them. Let’s show the simplest one, literal assignment:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-132"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-c github-light" data-language="c" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #e36209;">context</span><span class="token">[</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">aNumber</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">]</span><span class="token"> </span><span class="token" style="color: #d73a49;">=</span><span class="token"> @</span><span class="token" style="color: #005cc5;">22</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #6f42c1;">NSLog</span><span class="token">(</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #005cc5;">%d</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">,</span><span class="token"> </span><span class="token">[</span><span class="token" style="color: #e36209;">context</span><span class="token">[</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">aNumber</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">]</span><span class="token"> toInt32</span><span class="token">]</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-135"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-133">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-134'
	>
	The console outputs <strong>22</strong>.<br>Of course, we can also assign <code>JSValue</code>s, <code>NSNumber</code>s and so on. But this example reveals an important detail: it’s simple to access values within a context using the subscripting operator. We can also access values which were created or set by JavaScript code evaluated in the context. For example:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-137"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-c github-light" data-language="c" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token">[</span><span class="token">context evaluateScript:@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">var anotherNumber = aNumber + 20;</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">]</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #6f42c1;">NSLog</span><span class="token">(</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #005cc5;">%d</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">,</span><span class="token"> </span><span class="token">[</span><span class="token" style="color: #e36209;">context</span><span class="token">[</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">anotherNumber</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">]</span><span class="token"> toInt32</span><span class="token">]</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-140"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-138">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-139'
	>
	The console outputs <strong>42</strong>.<br>Here we see how JavaScript code can be executed within the context. The <code>aNumber</code> variable which we created earlier is also available to the JavaScript code. However, if we were to store it in a <code>JSValue</code>, the <code>JSValue</code> would only contain its current value, and not a reference to the variable in the JavaScript context. In other words, it <strong>wouldn’t change</strong> when the variable within the context changes, nor would the JavaScript variable change when the <code>JSValue</code> is changed.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-143"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-141">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-142'
	>
	Functions can also be <code>JSValue</code>s. We can make use of that to expose an Objective-C block to a JavaScript context. For example, JavaScript’s <code>console.log()</code> is not bridged automatically to the Xcode console. Here’s how we can enable logging:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-145"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-c github-light" data-language="c" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token">[</span><span class="token">context evaluateScript:@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">var console = {};</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">]</span><span class="token">;</span><span class="token" style="color: #6a737d;"> </span><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;"> make console an empty object
</span></span><span class="line"><span class="token" style="color: #e36209;">context</span><span class="token">[</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">console</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">]</span><span class="token">[</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">log</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">]</span><span class="token"> </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #d73a49;">^</span><span class="token">(</span><span class="token">NSString </span><span class="token" style="color: #d73a49;">*</span><span class="token">message</span><span class="token">)</span><span class="token"> </span><span class="token">{</span><span class="token">
</span></span><span class="line"><span class="token">    </span><span class="token" style="color: #6f42c1;">NSLog</span><span class="token">(</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #b31d28;font-style: italic;">%</span><span class="token" style="color: #032f62;">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">,</span><span class="token"> message</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">}</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-148"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-146">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-147'
	>
	If we then write <code>[context evaluateScript:@&amp;#8221;console.log(anotherNumber)&amp;#8221;];</code>, the console will output <strong>42</strong>, as expected. This example shows how to use two-level subscripting to create properties that can be accessed from JavaScript via dot notation. It also shows how an Objective-C block can be assigned to a variable in a JavaScript context and how it can be called from within the context. The block can, of course, return a value and have an arbitrary number of arguments.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-151"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-149">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-150'
	>
	But it can go the other way around, too! It’s possible to call a JavaScript function from Objective-C:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-153"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-c github-light" data-language="c" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token">[</span><span class="token">context evaluateScript:@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">var add = function(a, b) {return a + b;}</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">]</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #6f42c1;">NSLog</span><span class="token">(</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #b31d28;font-style: italic;">%</span><span class="token" style="color: #032f62;">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">,</span><span class="token"> </span><span class="token">[</span><span class="token" style="color: #e36209;">context</span><span class="token">[</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">add</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">]</span><span class="token"> callWithArguments:@</span><span class="token">[</span><span class="token">@</span><span class="token" style="color: #005cc5;">20</span><span class="token">,</span><span class="token"> @</span><span class="token" style="color: #005cc5;">30</span><span class="token">]</span><span class="token">]</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #6f42c1;">NSLog</span><span class="token">(</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #b31d28;font-style: italic;">%</span><span class="token" style="color: #032f62;">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">,</span><span class="token"> </span><span class="token">[</span><span class="token" style="color: #e36209;">context</span><span class="token">[</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">add</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">]</span><span class="token"> callWithArguments:@</span><span class="token">[</span><span class="token" style="color: #e36209;">context</span><span class="token">[</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">aNumber</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">]</span><span class="token">,</span><span class="token"> </span><span class="token" style="color: #e36209;">context</span><span class="token">[</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">anotherNumber</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">]</span><span class="token">]</span><span class="token">]</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-156"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-154">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-155'
	>
	The console will output <strong>50</strong> and <strong>64</strong>, as expected. Now let’s spice things up a bit with a more complicated function that still serves the same purpose:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-158"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-c github-light" data-language="c" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token">[</span><span class="token">context evaluateScript:@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">var addAndPrint = function(a, b) {console.log(add(a, b));}</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">]</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">[</span><span class="token" style="color: #e36209;">context</span><span class="token">[</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">addAndPrint</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">]</span><span class="token"> callWithArguments:@</span><span class="token">[</span><span class="token">@</span><span class="token" style="color: #005cc5;">20</span><span class="token">,</span><span class="token"> @</span><span class="token" style="color: #005cc5;">30</span><span class="token">]</span><span class="token">]</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">[</span><span class="token" style="color: #e36209;">context</span><span class="token">[</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">addAndPrint</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">]</span><span class="token"> callWithArguments:@</span><span class="token">[</span><span class="token" style="color: #e36209;">context</span><span class="token">[</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">aNumber</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">]</span><span class="token">,</span><span class="token"> </span><span class="token" style="color: #e36209;">context</span><span class="token">[</span><span class="token">@</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">anotherNumber</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">]</span><span class="token">]</span><span class="token">]</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-161"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-159">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-160'
	>
	The console output is the same as it was in the previous example. The <code>addAndPrint()</code> function uses the previously declared <code>add()</code> function as well as <code>console.log()</code> which maps to an Objective-C block.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-164"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-162">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-163'
	>
	What next?</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-167"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-165">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-166'
	>
	Thus far we have managed to create variables in the JavaScript context, assign values to them and call JavaScript functions and Objective-C blocks back and forth. While this is powerful by itself, there is more to JavaScriptCore.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-170"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-168">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-169'
	>
	A good example of what’s possible is the <code>JSExport</code> protocol. If a class implements it, all of its properties and methods can be used in JavaScript contexts. Protocol inheritance makes it possible to exclude unwanted properties or methods from this behavior.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-173"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-171">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-172'
	>
	This feature is awesome because it allows you to assign an object to a <code>JSValue</code> and expose all or a part of its properties and methods to JavaScript. It also bypasses the aforementioned limitation of <code>JSValue</code> regarding changes to a <code>JSValue</code> on Objective-C side not propagating to the respective JavaScript variable and vice-versa.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-176"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-174">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-175'
	>
	Using the <code>JSExport</code> protocol, it’s possible to have a reference to a mutable object both from Objective-C and JavaScript execution environments. The bridge between the two is implemented by JavaScript Core, so that each change to the object made in one execution environment gets copied immediately to the other one.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-179"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-177">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-178'
	>
	To explore this and other features and learn more about JavaScriptCore in general, refer to the WWDC session number 615, which targets this topic specifically.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-182"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-180">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-181'
	>
	If you decide to use the framework in a project, your JavaScript developers will probably want to know what they’re working with. To avoid confusion, here’s what JavaScriptCore’s developers say about the project:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-187"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="blockquote block-blockquote__blockquote" data-id="es-183">
	
	<div class="blockquote__content">
		<i
	class="icon blockquote__icon icon--size-16 icon--scale-100"
	 aria-hidden='true' data-name='blockquote-24' data-id='es-184'>
	<svg fill='none' height='24' viewBox='0 0 24 24' width='24' xmlns='http://www.w3.org/2000/svg'><path clip-rule='evenodd' d='m12 24c6.6274 0 12-5.3726 12-12 0-2.79685-.9568-5.37021-2.561-7.41062-.581.22951-1.0832.60583-1.5069 1.12898-.5132.60844-.7698 1.41969-.7698 2.43375v.07605h2.5789v5.59004h-5.6197v-5.01962c0-1.11547.154-2.06616.4619-2.85205.3336-.81125.757-1.48307 1.2702-2.01545.528-.52161 1.1175-.92155 1.7687-1.1998-2.0728-1.70651-4.7279-2.73128-7.6223-2.73128-6.62742 0-12 5.37258-12 12 0 6.6274 5.37258 12 12 12zm-3.53811-18.05347c-.30793.78589-.46189 1.73658-.46189 2.85205v5.01962h5.6197v-5.59004h-2.5789v-.07605c0-1.01406.2566-1.82531.7698-2.43375.5389-.63379 1.1804-1.05209 1.9245-1.2549v-2.28164c-.7441.07605-1.4626.25351-2.1555.53238-.6928.27887-1.3086.68449-1.84752 1.21688-.51321.53238-.9366 1.2042-1.27019 2.01545z' fill='currentColor' fill-rule='evenodd'/></svg></i><p	class='typography typography--size-24-text js-typography blockquote__quote'
	data-id='es-185'
	>
	JavaScriptCore is the built-in JavaScript engine for WebKit. (…) JavaScriptCore is often referred with different names, such as SquirrelFish and SquirrelFish Extreme. Within the context of Safari, Nitro and Nitro Extreme (the marketing terms from Apple) are also commonly used. However, the name of the project and the library is always JavaScriptCore.</p>
		<div class="blockquote__caption-wrap">
					</div>
	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-190"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-188">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-189'
	>
	You should also note that the framework comes with a caveat: JIT compilation is not supported, although it isn’t ruled out for future releases, either.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-193"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-191">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-192'
	>
	Why do it?</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-196"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-194">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-195'
	>
	Apart from executing JavaScript code in native iOS applications being interesting and cool, I think it can be useful as well. Apple’s example from the WWDC session suggests <strong>extending the application’s functionality</strong> by using “modules” or “plug-ins” written in JavaScript.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-199"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-197">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-198'
	>
	To demonstrate the idea, they developed a simple syntax highlighting tool and a set of JS plug-ins that implemented the actual syntax highlighting logic for each programming language. Using this approach, they were able to support new languages just by loading additional plug-ins.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-202"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-200">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-201'
	>
	I believe a similar model could also be used to provide extensibility in applications that accomplish tasks using the same basic function set. Who knows, soon we may even see a simple JavaScript IDE as an iOS application.</p></div>	</div>
</div>
</div>		</div>
	</div><p>The post <a href="https://infinum.com/blog/running-javascript-in-an-ios-application-with-javascriptcore/">Running JavaScript in an iOS Application with JavaScriptCore</a> appeared first on <a href="https://infinum.com">Infinum</a>.</p>
]]>
				</content:encoded>
			</item>
		
	</channel>
</rss>