实现在VBScript懒加载模块 [英] Implementing lazy-loaded modules in VBScript

查看:143
本文介绍了实现在VBScript懒加载模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前阵子,我需要一个解决方案,以进口三立图书馆在VBScript。

A while back, I needed a solution to sanely import libraries in VBScript.

的VBScript,仅供参考,不具有内置的导入功能。导入文件的传统方法是使用SSI,这转储includee的内容逐字到includer。这是低于最优的一些原因:有没有办法避免多包容,有没有办法来指定库目录等,所以,我写我自己的函数。这是相当简单,用 executeGlobal 用字典来跟踪导入模块和包装整个事情中的对象进行封装:

VBScript, for reference, has no build-in import capabilities. The traditional method of importing files is to use SSI, which dumps the contents of the includee verbatim into the includer. This is less-than-optimal for a number of reasons: there is no way to avoid multiple inclusion, there's no way to specify a library directory, etc. So I wrote my own function. It's fairly simple, using executeGlobal with a dictionary to keep track of imported modules and wrapping the whole thing in an object for encapsulation:

class ImportFunction
    private libraries_

    private sub CLASS_INITIALIZE
        set libraries_ = Server.createObject("Scripting.Dictionary")
    end sub

    public default property get exec (name)
        if not libraries_.exists(name) then
            ' The following line will find the actual path of the named library '
            dim lib_path: set lib_path = Path.resource_path(name & ".lib", "libraries")

            on error resume next
            ' Filesystem is a class of mine; its operation should be fairly obvious '
            with FileSystem.open(lib_path, "")
                executeGlobal .readAll
                if Err.number <> 0 then 
                    Response.write "Error importing library "
                    Response.write lib_path & "<br>"
                    Response.write Err.source & ": " & Err.description
                end if
            end with
            on error goto 0

            libraries_.add name, null
        end if
    end property
end class
dim import: set import = new ImportFunction

' Example:
import "MyLibrary"

总之,这个工程pretty很好,但它是一个大量的工作,如果我没有最终使用的库。我想使懒惰,从而使文件系统的搜索,加载和执行只如果当库实际使用完成。这是由每个库的功能是通过在相同的名称作为库的全球范围内一个单只对象访问的事实简化。例如:

Anyway, this works pretty well, but it's a lot of work if I don't end up using the library. I'd like to make it lazy, so that the filesystem search, loading, and executing are only done if and when the library is actually used. This is simplified by the fact that each library's features are accessed solely through a singleton object in global scope of the same name as the library. For example:

' StringBuilder.lib '

class StringBuilderClass ... end class

class StringBuilderModule
    public function [new]
        set [new] = new StringBuilderClass
    end function

    ...
end class
dim StringBuilder: set StringBuilder = new StringBuilderModule

&NBSP;

import "StringBuilder"
dim sb: set sb = StringBuilder.new

如此看来,明显的方法是懒惰的进口商来定义的StringBuilder作为一个对象,访问时,将加载StringBuilder.lib和替换本身。

So it seems that the obvious approach is for the lazy importer to define StringBuilder as an object that, when accessed, will load StringBuilder.lib and replace itself.

不幸的是,这是由VB脚本伤心缺少元编程构造犯了难。举例来说,有没有模拟到Ruby的的method_missing ,这将作出实施微不足道的。

Unfortunately, this is made difficult by VBScripts sad lack of metaprogramming constructs. For instance, there is no analogue to Ruby's method_missing, which would have made the implementation trivial.

我首先想到的是主进口使用函数 executeGlobal 来创建一个名为StringBuilder的不采取任何一个全局函数争论这反过来负荷StringBuilder.lib再与StringBuilder的单使用 executeGlobal 来的影子本身(功能)。有两个问题:第一,使用 executeGlobal 来定义一个函数,然后用 executeGlobal 似乎是一个覆盖本身一般比较粗略的想法;第二,事实证明,在VBScript中,你只能覆盖一个函数的变量,如果有问题的函数是一个内建命令。 Oooookay。

My first thought was for the main import function to use executeGlobal to create a global function named StringBuilder taking no arguments which would in turn load StringBuilder.lib and then use executeGlobal to "shadow" itself (the function) with the StringBuilder singleton. There are two problems with this: first, using executeGlobal to define a function which then overrides itself using executeGlobal seems like a rather sketchy idea in general, and second, it turns out that in VBScript, you can only override a function with a variable if the function in question is a builtin. Oooookay.

在下以为我在做同样的事情,但不是使用 executeGlobal 用一个变量来代替的功能,用它来替换另一个函数的函数简单地返回单。这将需要将单被存储在一个单独的全局变量。这种方法(除了战略的内在unkosherness)的缺点是,访问单会增加开销函数调用,由于国米preTER的解析怪癖,单身不再使用默认属性。

The next thought I had was doing the same thing, except instead of using executeGlobal to replace the function with a variable, use it to replace the function with another function which simply returned the singleton. This would require that the singleton be stored in a separate global variable. The disadvantages to this approach (aside from the inherent unkosherness of the strategy) are that accessing the singleton would add function call overhead and that, due to the interpreter's parsing eccentricities, the singleton could no longer use default properties.

总的来说,这是一个相当棘手的问题,和VBScript的奇怪怪癖是没有帮助。任何意见或建议,将受到欢迎。

Overall, it's a rather sticky problem, and VBScript's odd quirks are of no help. Any ideas or suggestions would be welcome.

推荐答案

应该Windows脚本组件的帮助吗? http://msdn.microsoft.com/en-us/库/ 07zhfkh8(VS.85)的.aspx

Would Windows Script Components help here? http://msdn.microsoft.com/en-us/library/07zhfkh8(VS.85).aspx

这基本上是写使用VBScript或JScript COM组件的方式,你可以使用实例的CreateObject

It's basically a way to write COM components using VBScript or JScript which you can instantiate using CreateObject

这篇关于实现在VBScript懒加载模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆