2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-05 08:59:27 +08:00

FEATURE: upgrade from therubyracer to mini_racer

This pushes our internal V8 JavaScript engine from Chrome 32 to 50.

It also resolves some long standing issues we had with the old wrapper.
This commit is contained in:
Sam 2016-05-19 22:25:08 +10:00 committed by Sam Saffron
parent f387dfe226
commit 695773db1c
7 changed files with 70 additions and 68 deletions

View file

@ -1,4 +1,4 @@
require 'v8'
require 'mini_racer'
require 'nokogiri'
require_dependency 'url_helper'
require_dependency 'excerpt_parser'
@ -7,7 +7,9 @@ require_dependency 'discourse_tagging'
module PrettyText
class Helpers
module Helpers
extend self
def t(key, opts)
key = "js." + key
unless opts
@ -81,6 +83,7 @@ module PrettyText
nil
end
end
end
@mutex = Mutex.new
@ -92,9 +95,11 @@ module PrettyText
def self.create_new_context
# timeout any eval that takes longer than 15 seconds
ctx = V8::Context.new(timeout: 15000)
ctx = MiniRacer::Context.new(timeout: 15000)
ctx["helpers"] = Helpers.new
Helpers.instance_methods.each do |method|
ctx.attach("helpers.#{method}", Helpers.method(method))
end
ctx_load(ctx,
"vendor/assets/javascripts/md5.js",
@ -198,6 +203,7 @@ module PrettyText
# we use the exact same markdown converter as the client
# TODO: use the same extensions on both client and server (in particular the template for mentions)
baked = nil
text = text || ""
protect do
context = v8
@ -206,8 +212,9 @@ module PrettyText
context_opts = opts || {}
context_opts[:sanitize] = true unless context_opts[:sanitize] == false
context['opts'] = context_opts
context['raw'] = text
context.eval("opts = #{context_opts.to_json};")
context.eval("raw = #{text.inspect};")
if Post.white_listed_image_classes.present?
Post.white_listed_image_classes.each do |klass|
@ -258,8 +265,10 @@ module PrettyText
# leaving this here, cause it invokes v8, don't want to implement twice
def self.avatar_img(avatar_template, size)
protect do
v8['avatarTemplate'] = avatar_template
v8['size'] = size
v8.eval <<JS
avatarTemplate = #{avatar_template.inspect};
size = #{size.to_i};
JS
decorate_context(v8)
v8.eval("Discourse.Utilities.avatarImg({ avatarTemplate: avatarTemplate, size: size });")
end
@ -267,9 +276,8 @@ module PrettyText
def self.unescape_emoji(title)
protect do
v8["title"] = title
decorate_context(v8)
v8.eval("Discourse.Emoji.unescape(title)")
v8.eval("Discourse.Emoji.unescape(#{title.inspect})")
end
end
@ -413,15 +421,7 @@ module PrettyText
def self.protect
rval = nil
@mutex.synchronize do
begin
rval = yield
# This may seem a bit odd, but we don't want to leak out
# objects that require locks on the v8 vm, to get a backtrace
# you need a lock, if this happens in the wrong spot you can
# deadlock a process
rescue V8::Error => e
raise JavaScriptError.new(e.message, e.backtrace)
end
rval = yield
end
rval
end